Given is string: dog apple orange banana
I need to make it: monkey apple cow banana
That is without calling sed twice.
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.
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 '.
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'
Use ;
to queue commands:
sed -e 's/dog/monkey/g;s/orange/cow/g'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With