【问题标题】:Command "Print All Lines Starting With" [closed]命令“打印所有以开头的行”[关闭]
【发布时间】:2012-09-16 04:20:15
【问题描述】:

我正在寻找一个打印以字符串开头的行的 perl 命令。例如,如果我想打印以“1234”开头的所有行,该命令会是什么样子?赛德?啊?格雷普?

【问题讨论】:

    标签: perl unix sed awk command


    【解决方案1】:
    perl -ne 'print if /^1234/' 
    

    sed -ne '/^1234/p'
    

    awk '/^1234/'
    

    grep '^1234'
    

    ruby -ne 'print if /^1234/'
    

    甚至

    python -c 'import fileinput;print "\n".join([l for l in fileinput.input() if l.startswith("1234")])'
    

    【讨论】:

    • 哦,蟒蛇真漂亮
    • python -c 'import sys; [sys.stdout.write(l) for l in sys.stdin if l.startswith("1234")]' -- 可能稍微不那么病态,但显然 Python 并不是要取代 grep :)
    • 感谢@Eevee 的改进。当然,使用 sys.stdin 不会让您在命令行末尾指定文件列表,就像我的答案中的其余条目那样... :) 但是,是的,这绝对不是 Python 的利基。
    【解决方案2】:
    perl -ne'print if /^1234/' file
    

    但大多数人会使用grep

    grep ^1234 file
    

    或在 Windows 上

    findstr /r ^1234 file
    

    【讨论】:

      【解决方案3】:

      完成:

      sed -ne '/^1234/p' somefile
      awk '/^1234/{print}' somefile
      grep '^1234' somefile
      

      【讨论】:

      • {print} 是 awk 的默认操作,因此您只需要 /^1234/ 部分。
      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多