How to replace multiple strings using sed?
Answer:
If you have multiple strings to replace, you can either using the "-e" option in sed, e.g.
sed -e 's/a/A/' -e 's/b/B/' < old.txt > new.txt
or, you can use a sed script file to store all the replace conditions
e.g. vi replace.sed
s/a/A/g
s/b/B/g
s/c/C/g
and execute like
sed -f replace.sed < old.txt > new.txt


Or you can include all in a single, newline-separated string:
sed 's/a/A/g
s/b/B/g
s/c/C/g' new.txt
Or
sed "s/str1/str2/g ; s/strA/strB/g ; s/foo/bar/g" file1 > file2
I was wondering if you could use a single sed s command to replace multiple different strings with one string. In my case I need to replace all punctuation marks with an underscore. I understand I could write multiple sed s commands for ".", "?" and "!". I was just wondering if there was a cleaner way to do it.
For those who want to know this is for an Applescript for finding and editing iTunes tracks since iTunes automatically changes punctuation in the artist, album, and track name to an "_".
@Mike
You can use character class, e.g.
echo "?,." | sed 's/[?,.]/_/g'