-n
参数导致 Perl 在程序周围放置一个循环,这使得它迭代文件名参数有点像 sed -n
或 awk
。
现在,是否可以使用 -n
在脚本中提前跳过抓取一些即将到来的行?
#!/usr/bin/perl -wn
if (/my case/) {
# Skip two lines
<>; <>;
# Do something with the line just read.
}
以上内容对我不起作用。 $_
卡在同一行/内容。
最佳答案
对我有用:
perl -wE 'say for 1..10' | perl -ne 'if (/2/) { <>; <>; print "!$_" } else { print }'
1
!2
5
6
7
8
9
10
如果要处理接下来的两行,将它们存储在变量中。
$line1 = <>; $line2 = <>;
<>
其自身不会填充 $_
- 仅在 while (<>)
的特殊情况下.
关于linux - "perl -n"时跳行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29416364/