Avatar of yasuakikudo
yasuakikudo
 asked on

How can I perform "Logical OR" in a regular expression in sed?

I would like to perform Logical OR operations in SED.

For instance, instead of s/[ab]/c/

I would like to do s/SentenceA|SentenceB/SentenceC/

Is this possible?
Shell Scripting

Avatar of undefined
Last Comment
Maciej S

8/22/2022 - Mon
Maciej S

Just as you wrote  it :)
There may be only need to escape '|' character with backslash.
sed 's/SentenceA\|SentenceB/SentenceC/'

If these sentences contains spaces:
sed 's/\(Sentence A\)\|\(Sentence B\)/Sentence C/'
ASKER CERTIFIED SOLUTION
McNetic

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
nognew

Hello mate!
 you can apply pattern multiple times. See if that sample can help:
echo "cat dog fox" |sed -e 's/dog/cat/g' -e 's/fox/cat/g'

Open in new window

yasuakikudo

ASKER
Thank you, I did not realise this!!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
McNetic

I think it's not fair that my answer got accepted alone - the first answer is also correct, you should at least consider splitting the points.
Maciej S

In case author have GNU sed, then I have to admit, that your answer is much better. No need to remember what should be escaped. I agree with author about chosen answer :)