2017-04-06 20 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