简体   繁体   English

Linux grep和特殊字符之间的打印

[英]Linux grep and print between special character

So this is how my data looks. 这就是我的数据。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
date        start     stop      durat  cond  acc_5  acc_6  dialled_number   outg_trunk  connected_num    inc_trunk   calling_num      charged_num      ring  que
60512-09-56  66:65:99  66:90:65  0081212  NI                  90000                        912690            0050060055  56012975125        56012975125        8     0
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
date        start     stop      durat  cond  acc_5  acc_6  dialled_number   outg_trunk  connected_num    inc_trunk   calling_num      charged_num      ring  que
60512-09-56  66:67:59  66:95:55  00955  I                   900012                        9120512            0700050005  901299120512        901299120512        0     0
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
date        start     stop      durat  cond  acc_5  acc_6  dialled_number   outg_trunk  connected_num    inc_trunk   calling_num      charged_num      ring  que
60512-09-56  66:67:60  66:95:55  009512  NJ                  90009            5000060069  05055                        9120512            912999            5     0

endpoint                endp_type     codec_5     codec_6     cum_pack_lost  pack_lost  frac_lost   mean_delay  worst_delay  mean_jitter  worst_jitter  R-value
50.9.0.56:57696         a_multiLim    PCMA                    0              0          0          0           65           0            0             996
-----------------------------------------------------------------------------------------------------  ------------------------------------------------------------

This is just an example of the data, it contains thousands of these lines. 这只是数据的一个示例,其中包含数千行。 And i want to find the data containing for example "900012" 我想查找包含例如“ 900012”的数据

So i get 所以我得到


date        start     stop      durat  cond  acc_5  acc_6  dialled_number   outg_trunk  connected_num    inc_trunk   calling_num      charged_num      ring  que
60512-09-56  66:65:99  66:90:65  0081212  NI                  90000                        912690            0050060055  56012975125        56012975125        8     0
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

I know i could do "cat file.txt | grep 900012 -A1 -B1" but some lines are more then 1 above and one under. 我知道我可以执行“ cat file.txt | grep 900012 -A1 -B1”,但是有些行的上方和下方分别是1和1。 so is there a way to tell grep to print the rows until it hits "-----" above and under then stop. 因此,有一种方法可以告诉grep打印行,直到它击到上下的“ -----”,然后停止。

I could do this simply in python or bash. 我可以简单地在python或bash中做到这一点。 But iam looking for a one line thing that i can just paste in bash, since i would use this in hundreds of systems. 但是我正在寻找一种可以粘贴在bash中的单行代码,因为我将在数百个系统中使用它。 And dont want to copy paste a file etc in to every system. 并且不想将粘贴文件等复制到每个系统中。

This can be a way with awk : 这可以是awk一种方式:

awk '/^--/ { if (f) {print line} {f=0; line=""; next}}
     /900012/ {f=1}
     {if (line) {line=line RS $0} else {line=$0}}' file

Explanation 说明

  • /^--/ { if (f) {print line} {f=0; line=""; next}} /^--/ { if (f) {print line} {f=0; line=""; next}} in lines starting with -- , it checks if the flag f is set or not. /^--/ { if (f) {print line} {f=0; line=""; next}}在以--开头的行中,它检查标志f是否已设置。 If so, prints the buffered data. 如果是这样,则打印缓冲的数据。 Then it unsets f , empties the buffered line and jumps to the next line. 然后取消设置f ,清空缓冲的行并跳至下一行。
  • /900012/ {f=1} if the line matches 900012 , then trigger the flag f . /900012/ {f=1}如果该行与900012匹配,则触发标志f
  • {if (line) {line=line RS $0} else {line=$0}} at any line, buffer the content in the variable line . {if (line) {line=line RS $0} else {line=$0}}在任意行上,将内容缓冲在变量line

For your sample input it returns: 对于您的样本输入,它返回:

$ awk '/^--/ { if (f) {print line} {f=0; line=""; next}} /900012/ {f=1} {if (line) {line=line RS $0} else {line=$0}}' file
date        start     stop      durat  cond  acc_5  acc_6  dialled_number   outg_trunk  connected_num    inc_trunk   calling_num      charged_num      ring  que
60512-09-56  66:67:59  66:95:55  00955  I                   900012                        9120512            0700050005  901299120512        901299120512        0     0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM