【问题标题】:What's the equivalent of sed -n 'np' file in perlperl 中 sed -n 'np' 文件的等价物是什么
【发布时间】:2014-04-26 01:34:02
【问题描述】:

通过sed,可以得到文件的第n行。

sed -n '40p' file;
A=$(sed -n '40p' file);

在 perl 中这个等价物是什么?

【问题讨论】:

  • 请注意,如果您想阅读特定行,最好退出,即改用sed -n '40pq'

标签: perl sed


【解决方案1】:
perl -ne '$. ==40 and print' file

或者您可以跳过少于 40 行,打印所需的行,并停止文件处理,

perl -ne 'next if $. <40; print; last' file

【讨论】:

  • 如果我想将它分配给数组或缩放变量怎么办?
  • @computer10 $line = $_ 而不是 print 如果要分配给某个变量。顺便说一句,你想分配给 shell 变量吗?
  • 我想将它分配给 perl 脚本中的变量。
  • 这相当于sed -n '40pq' :)
  • 我没有意识到s2p实际上做了很多。
【解决方案2】:

这可能是一个

perl -ne 'print if $. == 40'

$.是当前输入流的行号。

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2012-03-20
    • 2011-03-25
    • 2023-04-04
    • 2012-09-06
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多