【问题标题】:Grep all lines starting without # (hash) or greedy space and # (hash)grep 所有没有 # (散列)或贪婪空格和 # (散列)开头的行
【发布时间】:2017-02-18 22:19:49
【问题描述】:

我正在尝试 grep 不以开头的 conf 文件的所有有效行

  • 哈希 (或)
  • 任意数量的空格(0 个或多个)和一个哈希

下面的正则表达式似乎不起作用。

grep ^[^[[:blank:]]*#] /opt/logstash/logstash.conf

grep ^[^[[[:blank:]]*#]] /opt/logstash/logstash.conf

grep ^[^[\s]*#] /opt/logstash/logstash.conf

我试图了解我做错了什么。

FWIW,以下正则表达式有效:

grep -v '^[[:blank:]]*#' /opt/logstash/logstash.conf

【问题讨论】:

    标签: regex linux grep regex-negation regex-greedy


    【解决方案1】:

    首先,如果你不引用你的正则表达式,shell 可能会在 grep 看到它之前扩展它,所以你应该总是引用你的 grep 正则表达式。

    其次,它们不起作用的原因是:[],“括号表达式”,匹配它包含的任何元素,或者在使用 [^...] 时匹配它们的补码。你似乎试图匹配一个否定的字符序列,但这不是[] 所做的。

    此外,在括号表达式中,字符类不应使用双括号:[^[:blank:]] 而不是 [^[[:blank:]]]

    我不确定是否可以不使用 -v 并且仅使用基本或扩展正则表达式,但是使用可以执行 Perl 正则表达式的 grep(例如 GNU grep),您可以使用否定前瞻:

    grep -P '^(?![[:blank:]]*#)' /opt/logstash/logstash.conf
    

    这是您的正则表达式失败的原因(假设它们被引用):

    • ^[^[[:blank:]]*#] - 匹配任何以零个或多个字符开头的行,而不是文字 [ 或空白,后跟 #]
    • ^[^[[[:blank:]]*#]] - 匹配任何以零个或多个字符开头的行,而不是文字 [ 或空白(括号表达式中重复的 [[ 与单个 [ 相同),后跟 #]]
    • ^[^[\s]*#] - 匹配除[\s\s[] 中不特殊)以外的零个或多个字符开头的任何行,然后是#]

    如果我们拿这个测试文件:

    # comment
    normal line
        normal line after spaces
        # comment after spaces
        normal line after tab
        # comment after tab
    abc#] does not start with blank or [ and has #]
    [abc#] starts with [ and has #]
    abc#]] does not start with blank or [ and has #]]
     abc#]] starts with blank and has #]]
    sabc#]] starts with s and has #]]
    \abc#]] starts with \ and has #]]
    

    表达式匹配如下:

    • 您的grep -v(有效):

      $ grep -v '^[[:blank:]]*#' infile
      normal line
          normal line after spaces
          normal line after tab
      abc#] does not start with blank or [ and has #]
      [abc#] starts with [ and has #]
      abc#]] does not start with blank or [ and has #]]
       abc#]] starts with blank and has #]]
      sabc#]] starts with s and has #]]
      \abc#]] starts with \ and has #]]
      
    • 我的grep -P(工作):

      $ grep -P '^(?![[:blank:]]*#)' infile
      normal line
          normal line after spaces
          normal line after tab
      abc#] does not start with blank or [ and has #]
      [abc#] starts with [ and has #]
      abc#]] does not start with blank or [ and has #]]
       abc#]] starts with blank and has #]]
      sabc#]] starts with s and has #]]
      \abc#]] starts with \ and has #]]
      
    • 你的第一次尝试:

      $ grep '^[^[[:blank:]]*#]' infile
      abc#] does not start with blank or [ and has #]
      abc#]] does not start with blank or [ and has #]]
      sabc#]] starts with s and has #]]
      \abc#]] starts with \ and has #]]
      
    • 你的第二次尝试:

      $ grep '^[^[[[:blank:]]*#]]' infile
      abc#]] does not start with blank or [ and has #]]
      sabc#]] starts with s and has #]]
      \abc#]] starts with \ and has #]]
      
    • 你的第三次尝试:

      $ grep '^[^[\s]*#]' infile
      abc#] does not start with blank or [ and has #]
      abc#]] does not start with blank or [ and has #]]
       abc#]] starts with blank and has #]]
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-26
      • 2021-03-04
      • 2013-08-23
      • 2014-05-19
      • 2011-03-13
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      相关资源
      最近更新 更多