Terminal 101: Find and Replace Using Sed



+ Add a Comment

Sergio Arroyo

Is there a way to do several search/finds with one command on a Mac with sed, just like they do in Linux. I am a Spanish speaker and I want to write some pages in Spanish, but some online html editing engines (namely tumblr) won't recognize or interpret my letters with accents and tilde (i.e: "Español", canción, etc.) as such, so I'm getting gibberish as input. Therefore I have to use the html codes: & aacute; for á, & eacute; for é, & ntilde; for ñ and so on. (EDIT: The "code" tag doesn't seem to work in maclife right now, I had to separate "&" from "eacute;" and so on.)

I found this script which will let you do all of the required changes to the code with one single command:

cp $archivo ${archivo}.bak && cat ${archivo}.bak | sed -e ‘s/á/\á/g’ -e ‘s/é/\é/g’ -e ‘s/í/\í/g’ -e ‘s/ó/\ó/g’ -e ‘s/ú/\ú/g’ -e ‘s/ñ/\ñ/g’ -e ‘s/Á/\Á/g’ -e ‘s/É/\É/g’ -e ‘s/Í/\Í/g’ -e ‘s/Ó/\Ó/g’ -e ‘s/Ú/\Ú/g’ -e ‘s/Ñ/\Ñ/g’ -e ‘s/©/\©/g’ -e ‘s/¡/\¡/g’ -e ‘s/§/\§/g’ -e ‘s/ª/\ª/g’ -e ‘s/«/\«/g’ -e ‘s/®/\®/g’ -e ‘s/±/\±/g’ -e ‘s/²/\²/g’ -e ‘s/³/\³/g’ -e ‘s/¶/\¶/g’ -e ‘s/º/\º/g’ -e ‘s/»/\»/g’ -e ‘s/¿/\¿/g’ -e ‘s/Æ/\Æ/g’ -e ‘s/Ç/\Ç/g’ -e ‘s/æ/\æ/g’ -e ‘s/ç/\ç/g’ -e ‘s/Ü/\Ü/g’ -e ‘s/ü/\ü/g’ > $archivo

Bad thing is it will only work with sed for Linux. Is there a way you could do something like this on a Mac? Thanks a lot for your help!

Log in to Mac|Life directly or log in using Facebook

Forgot your username or password?
Click here for help.

Login with Facebook
Log in using Facebook to share comments and articles easily with your Facebook feed.