【问题标题】:Match lines that start with specifc letters and exclude certain words [closed]匹配以特定字母开头并排除某些单词的行[关闭]
【发布时间】:2013-10-24 19:11:19
【问题描述】:

我有一个文件,其中包含如下一系列行:

dbxxx
dbxxxx
dbxx
tdxx
tdxxx
sbxx
sbxxxxx
dbxx_migrated
tdxxx_old

其中 x = 一位数。

我需要创建一个输出,以确保显示以下开头的行:db、td 和 sb,并排除任何具有 _migrated 和 _old 的行。

使用 grep 可以做到这一点吗?

【问题讨论】:

  • 是的,有可能。 info grep 是你的朋友。

标签: linux bash shell scripting grep


【解决方案1】:

您可以将正则表达式和逻辑运算符与awk结合起来:

$ awk '/^(db)|(td)|(sb)/ && !/_(old)|(migrated)/' file
dbxxx
dbxxxx
dbxx
tdxx
tdxxx
sbxx
sbxxxxx

【讨论】:

    【解决方案2】:
    grep -E '^(db|td|sb)' file | grep -Ev '_(migrated|old)$'
    

    perl -lne 'print if /^(db|td|sb)/ and not /_(migrated|old)$/' file
    

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      相关资源
      最近更新 更多