80
12
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 ?
8The standard BSD/OS X version of
sed
does support alternation, but only with "extended" regex syntax (-E
) - which means no backslashes on either the pipes or the parentheses:echo "blia blib bou blf" | sed -E 's/bl(ia|f)//g'
– Mark Reed – 2014-09-30T17:42:47.0272I edited my answer to note that it's for GNU sed only. – sleske – 2015-07-14T10:57:40.120
36For anyone else confused by this answer | only works in gnu sed (gsed on os x) not vanilla sed (sed on os x). – Andrew Hancox – 2012-04-04T14:54:43.047
@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 – 2013-02-01T20:03:51.173