Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 :)

share|improve this question

5 Answers 5

up vote 31 down vote accepted

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 :-).



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).

share|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
For anyone else confused by this answer \| only works in gnu sed (gsed on os x) not vanilla sed (sed on os x). –  Andrew Hancox Apr 4 '12 at 14:54
@AndrewHancox Thank you so much! I was about to rip all of the hair out of my head (and so far I'm doing pretty good compared to my manager on the hair-front) - I know I know RegEx enough to try | and \| but I never thought about the fact that OSX might actually use a non-gnu sed. –  phatskat Feb 1 '13 at 20:03
Thanks Sleke, as well as Andrew, Phtskat, Joe and Daniel. –  Cedric May 28 '13 at 19:11

The \| does not work with sed on Solaris 10 either. What I did was use

perl -p -e 's/bl(ia|f)//g'
share|improve this answer
+1 for portability since, if a system has perl, it will always use this syntax, unlike sed. –  evilsoup May 30 '13 at 1:30

Since there are several comments regarding non-Gnu sed implementations: At least on OS X, you can use the -E argument to sed:

Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats.

Then you can use regular expression metacharacters without escaping them. Example:

$ echo "blia blib bou blf" | sed -E 's/bl(ia|f)//g'
 blib bou 
share|improve this answer

Followup: sed -E allows it on MacOS. No backslash need for |.

 sed -E 's/this|orthat/oooo/g' infile
share|improve this answer

GNU sed also supports the -r option (extended regular expressions). This means you don't have to escape the metacharacters:

echo foohello barhello | sed -re "s/(foo|bar)hello/hi/g"


hi hi
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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