加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 06:07:33 所属栏目:大数据 来源:网络整理
导读:我有一个名为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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读