I want to extract filter rules configured in /etc/lvm/lvm.conf
, like filter = [ \"r|/dev/sda|\" ]
. I want sed
to return \"r|/dev
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
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 ad
.
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|"
In case grep solution is acceptable :
grep -oP 'filter.*\K".*?"' inputfile