10-03-04 07:48 AM
Juhan Leemet <juhan@logicognosis.com> wrote:
> Hmm, when I tried this in bash on SuSE Linux, I found that I had to escape
> the '|' characters. I get confused with when to escape and when not to
> escape. I found that this works:
>
> sed -e "s/^\(FIRST\|SECOND\|THIRD\)$/-match-/"
That escaping is needed for sed, not for the shell. You can see what
sed saw like this:
$ printf %s\\n "s/^\(FIRST\|SECOND\|THIRD\)$/-match-/"
> I also had to bracket the alternation with parens, otherwise it seems that
> the beginning of line caret applied only to FIRST? and the end of line $
> applied only to THIRD?
Right: concatenation (such as between "^" and "F") binds things
together more strongly than alternation.
> BTW, it does not matter whether the quotes are single (') or double
> (").
True in this case. In general, it's easier to use single quotes if
you aren't doing any substitution. In double quotes, "\" and "$" are
sometimes special to the shell, depending on what comes after them.
> I guess the '|' is considered a normal character unless escaped?
> like the parens "()".
Yes; sed uses basic regular expressions. For extended regular
expressions, "|()" are special when unescaped, and literal when
escaped. You also get some extras like "+" with extended regular
expressions.
paul
[ Post a follow-up to this message ]
|