我有一个名为file的文件有三行:
line one
line two
line three
当我这样做:
perl -ne ‘print if /one/’ file
我得到这个输出:
line one
当我尝试这个:
perl -pe ‘next unless /one/’ file
输出是:
line one
line two
line tree
我期望两个单线的输出相同.我的期望是错的还是错的?
解决方法
你的期望是错误的. -p开关在代码周围放置以下循环:
LINE: while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\n"; }
如果您阅读the documentation for next
,它会说:
Note that if there were a
continue
block on the above,it would get
executed even on discarded lines.
接下来实际上跳到continue block(如果有的话),然后再回到循环的状态.
你可以做点什么
perl -pe '$_ = "" unless /one/' file