Replacing multiple strings with sed

While I’m the guy that effectively lives on the command-line, I still rarely use
sed, a stream editor. As I’ve been trying to use it more, I’ve run into some gotchas, mostly around the regular expression syntax and doing multiple replacements at one time.

Fortunately, both are easy enough to accomplish.

Doing multiple replacements can be done simply by chaining commands together:

sed --in-place='.bak' 's/match1/replace1/g; s/match2/replace2/g' \

If you want to replace multiple strings with one value, you can use an extremely escaped version of a regular expression:

sed --in-place='.bak' 's/\(first\|second\)/next/g' /tmp/some-file
Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

If you found this article helpful, please consider buying me a coffee.