Sed failed to match non whitespace characters with character class

前端 未结 3 863
名媛妹妹
名媛妹妹 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条回答
  • 2021-01-15 07:01

    Alternatively easier and shorter than [^[:space:]] you can do with \S+ without using brackets []

    \S means non whitespace char

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

    https://ideone.com/PxDX1Q

    0 讨论(0)
  • 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|"

    0 讨论(0)
  • 2021-01-15 07:18

    In case grep solution is acceptable :

    grep -oP 'filter.*\K".*?"' inputfile
    
    0 讨论(0)
提交回复
热议问题