up vote 2 down vote favorite
1
share [g+] share [fb]

I am using sed, GNU sed version 4.2.1. I want to use the alternation "|" symbol in a subexpression. For example :

echo "blia blib bou blf" | sed 's/bl\(ia|f\)//g'

should return

" blib bou "

but it returns

"blia blib bou blf".

How can I have the expected result ? I'd be thankful for any clues :)

link|improve this question
feedback

1 Answer

The "|" also needs a backslash to get its special meaning.

echo "blia blib bou blf" | sed 's/bl\(ia\|f\)//g'

will do what you want.

As you know, if all else fails, read the manual :-).

http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?%28sed.info.gz%29Regular%2520Expressions

`REGEXP1\|REGEXP2'

Matches either REGEXP1 or REGEXP2.

Note the backslash...

Unfortunately, regex syntax is not really standardized... there are many variants, which differ among other things in which "special characters" need \ and which do not. In some it's even configurable or depends on switches (as in GNU grep, which you can switch between three different regex dialects).

link|improve this answer
thank you very much ! – Cedric Feb 22 '10 at 16:46
@Cedric: Consider accepting my answer (click on checkmark next to it) if you think it is right. – sleske Feb 22 '10 at 17:02
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.