Sed failed to match non whitespace characters with character class

前端 未结 3 870
名媛妹妹
名媛妹妹 2021-01-15 06:29

I want to extract filter rules configured in /etc/lvm/lvm.conf, like filter = [ \"r|/dev/sda|\" ]. I want sed to return \"r|/dev

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-15 07:07

    Acc. to regular-expressions.info:

    One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression. So in POSIX, the regular expression [\d] matches a \ or a d.

    So you need to replace [^\s] with [^[:space:]] (any char other than whitespace).

    Example:

    echo ' filter = [ "r|/dev/sda|" ] ' | sed -E 's:^\s*filter\s*=\s*\[\s*([^[:space:]]+)\s*\]:\1:g'
    

    Output: "r|/dev/sda|"

提交回复
热议问题