understanding sed regex with numbered groups

2019-09-04 06:07发布

问题:

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.

回答1:

\(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 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 |.



回答2:

[^...] 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.



标签: regex linux sed