2017-04-06 48 views
0

例如,在如何打印my.txt containts的Perl:特定行

a 
b 
xx 
c 
d 

我想從以下行的第二行包含XX

我試圖打印

perl -nle 'if(/xx/){$n=$.};print if $.>($n+1)' my.txt 

但它沒有工作。它只是打印所有行。

+3

'的perl -ne '打印如果(/ XX/.. 0)> 1' my.txt' => http://perldoc.perl.org/perlop.html #Range-Operators –

+0

試試這個:'perl -ne'if(/ xx /){$ n = 1; next};打印if $ n'my.txt' –

+0

嗨,@Сухой27非常感謝。我閱讀了文檔。但我仍然不明白爲什麼'..'表達式可以與一個數字相比較?因爲doc說'..'是布爾值 – user15964

回答

0

在定義$ n之前,它被解釋爲0(零),意味着$。 xx之前還會打印> 1。這可能是你想要的東西:

perl -nle 'if(/xx/){$n=$.}; print if defined($n) and $. > $n+1' my.txt