use of alternation "|" in sed's regex

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 ?

6 Answers

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

GNU sed user's manual, section 3.3 Overview of Regular Expression Syntax:

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

This answer in particular is for GNU sed. There are other sed variants, for example the one used in the BSDs, which behave differently.

4

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 

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" 

Output:

hi hi 
1

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

perl -p -e 's/bl(ia|f)//g' 
1

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

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

In the GnuWin32 on Windows sed the syntax is sed "s/thing1\|thing2/ /g" source > destination.

The quotes must of type " - this is "Required" for the command to be parsed.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like