I'm trying to understand this sed regex.
sed 's/.*\(ADDR=[^|]*\) |.*/\1/'
If I'm not wrong, the above will search for the pattern ADDR=<something>
anywhere in a line and
replace it with the first group. I don't get the meaning of [^|] here. Thanks for any help.
\(ADDR=[^|]*\) |.*/\1/
Here
[^|]
matches anything other than|
and the quantifier*
quantifies zero or more occurrences of it.^
in character class negates the character class.|
matches the character|
NOTE In
sed
metacharacters like|
(
)
etc will lose its meaning so|
is not an alternation but matches a|
character. If you want to treat the metacharacters as such, then-r
(extended regular expression) will do so (with GNUsed
; use-E
with BSDsed
). Or escape\|
.Example:
Here
(ADDR=[^|]*\)
matches fromADDR= hello
which contains anything other than|
.[^...]
Matches any single character that is not in the class.|
The vertical bar separates two or more alternatives. A match occurs if any of the alternatives is satisfied. For example,gray|grey
matches bothgray
andgrey
.[^|]
matches anything other than a|
.^
in character class negates the character class while|
is loose it's actual meaning when using withsed
.