2014-06-15 52 views
0

使用awk僅抽取包含「A 0」,「A 1」,「A 2」,「A 3」的連續行的 是否可行? , 「A 4」,並從具有如下結構的文件 「A 5」 :grep遵循文件模式的連續行的具體數量

.......................................................................... 
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 
+0

一切皆有可能** **。你有沒有嘗試過幾乎沒有用的東西?顯示你的嘗試,並明確你需要幫助。 –

回答

1

awk,因爲你可以看看具體的專欄,而不是更一般的grep che怎麼樣確定第3列是「A」,第4列是從0到5循環?這將是

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

即使將第二行更改爲「A0」,也會打印第一行 – Ashkan

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