Sed failed to match non whitespace characters with character class

拟墨画扇 提交于 2021-01-19 06:32:31

问题


I want to extract filter rules configured in /etc/lvm/lvm.conf, like filter = [ "r|/dev/sda|" ]. I want sed to return "r|/dev/sda|". So I have tried the following script:

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

But it didn't work, the script has returned filter = [ "r|/dev/sda|" ]. I've tried a few on line regex tester, the group has been matched correctly.

However, if I replace [^\s]+ by .+, it works.

Doesn't [^\s]+ mean more than one non whitespace characters ?

Any idea please?


回答1:


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|"




回答2:


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




回答3:


In case grep solution is acceptable :

grep -oP 'filter.*\K".*?"' inputfile


来源:https://stackoverflow.com/questions/42249971/sed-failed-to-match-non-whitespace-characters-with-character-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!