in Command-line Interface

Replacing multiple strings with sed

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