grep跟随文件模式的特定连续行数

时间:2014-06-15 05:27:21

标签: awk

使用awk仅提取是否可行 连续的行包含" A 0"," A 1"," A 2"," A 3"," A 4& #34;和" A 5" 来自具有以下结构的文件:

..........................................................................
LINE  6295  A 0  LESS L6179     214.617 184.380  50.086  1.00  0.00      L     
LINE  6296  A 1  LESS L6179     215.525 185.163  49.136  1.00  0.00      L     
LINE  6297  A 2  LESS L6179     215.265 186.663  49.269  1.00  0.00      L     
LINE  6298  A 3  LESS L6179     217.012 184.776  49.298  1.00  0.00      L     
LINE  6299  A 4  LESS L6179     217.659 185.089  50.654  1.00  0.00      L     
LINE  6300  A 5  LESS L6179     219.029 184.429  50.766  1.00  0.00      L      
LINE  6316  A 0  LESS L6180     222.617 184.380  50.086  1.00  0.00      L     
LINE  6317  A 1  LESS L6180     223.525 185.163  49.136  1.00  0.00      L     
LINE  6320  A 4  LESS L6180     225.659 185.089  50.654  1.00  0.00      L     
LINE  6344  A 0  LESS L6222     158.617 192.380  50.086  1.00  0.00      L     
LINE  6345  A 1  LESS L6222     159.525 193.163  49.136  1.00  0.00      L     
LINE  6346  A 2  LESS L6222     159.265 194.663  49.269  1.00  0.00      L     
LINE  6347  A 3  LESS L6222     161.012 192.776  49.298  1.00  0.00      L     
LINE  6348  A 4  LESS L6222     161.659 193.089  50.654  1.00  0.00      L     
LINE  6349  A 5  LESS L6222     163.029 192.429  50.766  1.00  0.00      L      
LINE  6367  A 2  LESS L6223     167.265 194.663  49.269  1.00  0.00      L     
LINE  6368  A 3  LESS L6223     169.012 192.776  49.298  1.00  0.00      L     
LINE  6369  A 4  LESS L6223     169.659 193.089  50.654  1.00  0.00      L     
LINE  6370  A 5  LESS L6223     171.029 192.429  50.766  1.00  0.00      L    
..........................................................................

提供此类输出的东西:

LINE  6295  A 0  LESS L6179     214.617 184.380  50.086  1.00  0.00      L     
LINE  6296  A 1  LESS L6179     215.525 185.163  49.136  1.00  0.00      L     
LINE  6297  A 2  LESS L6179     215.265 186.663  49.269  1.00  0.00      L     
LINE  6298  A 3  LESS L6179     217.012 184.776  49.298  1.00  0.00      L     
LINE  6299  A 4  LESS L6179     217.659 185.089  50.654  1.00  0.00      L     
LINE  6300  A 5  LESS L6179     219.029 184.429  50.766  1.00  0.00      L   
LINE  6344  A 0  LESS L6222     158.617 192.380  50.086  1.00  0.00      L     
LINE  6345  A 1  LESS L6222     159.525 193.163  49.136  1.00  0.00      L     
LINE  6346  A 2  LESS L6222     159.265 194.663  49.269  1.00  0.00      L     
LINE  6347  A 3  LESS L6222     161.012 192.776  49.298  1.00  0.00      L     
LINE  6348  A 4  LESS L6222     161.659 193.089  50.654  1.00  0.00      L     
LINE  6349  A 5  LESS L6222     163.029 192.429  50.766  1.00  0.00      L 

2 个答案:

答案 0 :(得分:1)

awk中,由于您可以查看特定列,而不是更一般的grep如何检查第3列是“A”而第4列是从0到5循环?那将是

awk 'BEGIN{seek=0} $3=="A" && $4==seek {print; seek +=1; if (seek>5) seek=0}' file.txt

答案 1 :(得分:0)

awk '$3=="A" && $4==s {A[s]=$0; s++; if (s<6) next;}
      s==6 {for(i=0; i in A; i++) print A[i]; s=0; next;}
      {s=0}' input.txt

输出:

LINE  6295  A 0  LESS L6179     214.617 184.380  50.086  1.00  0.00      L
LINE  6296  A 1  LESS L6179     215.525 185.163  49.136  1.00  0.00      L
LINE  6297  A 2  LESS L6179     215.265 186.663  49.269  1.00  0.00      L
LINE  6298  A 3  LESS L6179     217.012 184.776  49.298  1.00  0.00      L
LINE  6299  A 4  LESS L6179     217.659 185.089  50.654  1.00  0.00      L
LINE  6300  A 5  LESS L6179     219.029 184.429  50.766  1.00  0.00      L
LINE  6344  A 0  LESS L6222     158.617 192.380  50.086  1.00  0.00      L
LINE  6345  A 1  LESS L6222     159.525 193.163  49.136  1.00  0.00      L
LINE  6346  A 2  LESS L6222     159.265 194.663  49.269  1.00  0.00      L
LINE  6347  A 3  LESS L6222     161.012 192.776  49.298  1.00  0.00      L
LINE  6348  A 4  LESS L6222     161.659 193.089  50.654  1.00  0.00      L
LINE  6349  A 5  LESS L6222     163.029 192.429  50.766  1.00  0.00      L