0

To change all occurences of the string foo to bar in all files in a directory I use

sed -i -- 's/foo/bar/g' *

Found that here

But I also want to change NAME to name etc. I tried,

sed -i -- 's/foo/bar/g' * ; sed -i -- 's/NAME/name/g' *

This creates new files. How can I get this command to rewrite the original file with foo replaced with bar and also NAME replaced with name and then there would be other replacements, without creating new files?

8

You can give several expressions to sed in one invocation:

sed -e 'expr' -e 'expr' -e ...

In your case:

sed -e 's/foo/bar/g' -e 's/NAME/name/g'

The expressions will be applied to each line of input in succession, from left to right.

  • Thank you that is exactly what I was looking for. thanks again – Shane G Sep 5 '17 at 9:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.