【问题标题】:Find lines where lines start with string查找以字符串开头的行
【发布时间】:2022-08-11 23:07:22
【问题描述】:

我需要在文本文件中找到第二列以数字117 开头的行。我如何在 bash 中做到这一点?我试过了

grep -E 117 test.txt

但这只是用117 打印所有内容。

  • 使用awk \'$2 ~ /^117/\' file
  • 你的字段分隔符是什么?

标签: bash


【解决方案1】:

grep 不是用于此目的的正确工具。使用以下 2 个 awk 解决方案之一:

# regex approach
awk '$2 ~ /^117/' test.txt

# non-regex approach
awk 'index($2, "117") == 1' test.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多