perl – 为什么我的-p one-liner打印出我想跳过的行?

perl – 为什么我的-p one-liner打印出我想跳过的行?,第1张

概述我有一个名为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 我期望两个单线的输出相同.我的期望是错的还是 我有一个名为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
总结

以上是内存溢出为你收集整理的perl – 为什么我的-p one-liner打印出我想跳过的行?全部内容,希望文章能够帮你解决perl – 为什么我的-p one-liner打印出我想跳过的行?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1213590.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存