Understanding sed regex with numbered groups

I am trying to understand this regex.

       sed 's/.*\(ADDR=[^|]*\) |.*/\1/'

      

If I am not mistaken, the above will search for the pattern ADDR=<something>

anywhere in the string and replace it with the first group. Here I don't understand the meaning of [^ |] . Thanks for any help.

0


source to share


2 answers


\(ADDR=[^|]*\) |.*/\1/

Here

  • [^|]

    matches anything other than |

    , and the quantifier *

    specifies zero or more of its occurrences. ^

    in a character class negates the character class.

  • |

    matches the character |

NOTE In sed

metacharacters like |

(

)

etc. lose their meaning, therefore |

it is not an interleaving, but matches a symbol |

. If you want to treat metacharacters as such, then -r

(extended regex) will do it (with GNU sed

, use -E

with BSD sed

). Or run \|

.



Example:

$ echo "hello ADDR= hello | world " | sed 's/.*\(ADDR=[^|]*\) |.*/\1/'
ADDR= hello

      

Here (ADDR=[^|]*\)

matches ADDR= hello

that contains anything other than |

.

+2


source


[^...]

Matches any single character that does not belong to a class.

|

A vertical bar separates two or more alternatives. Matching occurs if any of the alternatives are met. For example, gray|grey

matches both gray

and grey

.



[^|]

matches anything other than a |

. ^

in a character class negates the character class and |

is lost in real meaning when used with sed

.

+2


source







All Articles