Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace two things at once with sed?

Tags:

shell

sed

Given is string: dog apple orange banana

I need to make it: monkey apple cow banana

That is without calling sed twice.

like image 345
rsk82 Avatar asked May 12 '12 17:05

rsk82


People also ask

How do you sed multiple times?

You can tell sed to carry out multiple operations by just repeating -e (or -f if your script is in a file). sed -i -e 's/a/b/g' -e 's/b/d/g' file makes both changes in the single file named file , in-place.

Which sed command is used for replacement?

The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ' s/ regexp / replacement / flags '.


2 Answers

The following sed example should solve your problem. sed allows multiple -e switches, which allows you to replace more than one thing at a time.

sed -e 's/dog/monkey/g' -e 's/orange/cow/g'
like image 173
Yasser Zamani Avatar answered Oct 19 '22 01:10

Yasser Zamani


Use ; to queue commands:

sed -e 's/dog/monkey/g;s/orange/cow/g'
like image 51
user123444555621 Avatar answered Oct 19 '22 01:10

user123444555621