gpt4 book ai didi

regex - Grep 所有不以#(哈希)或贪心空格和#(哈希)开头的行

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:39 27 4
gpt4 key购买 nike

我正在尝试 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

最佳答案

首先,如果你不引用你的正则表达式,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 #]]

关于regex - Grep 所有不以#(哈希)或贪心空格和#(哈希)开头的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com