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
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 GNU sed
; use -E
with BSD sed
). Or escape \|
.
Example:
$ echo "hello ADDR= hello | world " | sed 's/.*\(ADDR=[^|]*\) |.*/\1/'
ADDR= hello
Here (ADDR=[^|]*\)
matches from ADDR= 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 both gray
and grey
.
[^|]
matches anything other than a |
. ^
in character class negates the character class while |
is loose it's actual meaning when using with sed
.