2010-10-27 83 views
0

如何打印在第n行匹配模式之後的所有行,但忽略所有匹配的前行,包括匹配行,這裏有一個例子:打印所有行下面匹配

row1 something in this row 
row2 something in this row 
row3 something in this row 
row4 don't need to match the whole line, something like java String.contains() 
row5 something in this row 
row6 something in this row 
row7 something in this row 
row8 something in this row 
row9 something in this row 
row10 something in this row 

我期待打印行,其中包含行4中的任何位置的行,這是可能的awk或sed,或任何其他方式?

輸出應該是:

row5 something in this row 
row6 something in this row 
row7 something in this row 
row8 something in this row 
row9 something in this row 
row10 something in this row 

我已經看到了類似的問題:

Awk - print next record following matched record

但我不知道如何去適應它適合我的需要。

回答

0

您的使用下面的命令:

grep -A 5 "row4" test.txt 

輸出將是:

row4 
row5 
row6 
row7 
row8 
row9 

-A無二後和與之相似,您可以使用-B鍵這也適用於以前。 5是要輸出的行數。你當然可以選擇你想要的任何東西。

0

這個工作,一個大師也許可以縮短相當:

#!/usr/bin/perl 
$pat = shift(@ARGV); 
$p = 0; 
while(<>) { 
    if ($p == 1) { print $_;} 
    if ($_ =~ /$pat/) { 
    $p = 1; 
    } 
} 

從CMDLINE:

$ ./p.pl dat4 datafile 
0
awk -v pattern="row4" 'go {print} $0 ~ pattern {go = 1}' input_file 
2

試試這個:

sed '1,/row4/d' inputfile 
相關問題