Replacing multiple strings with sed

Josh Sherman
1 min read
Command-line Interface

While I’m the guy that effectively lives on the command-line, I still rarely use sed. 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' /tmp/some-file

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
Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

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

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles