Posts Tagged ‘IRC’

Linux Search and Replace using Find and SED

Friday, May 2nd, 2008

First of all I want to give a big thanks to ek (Janky - check out his FreeBSD page) from the Ubuntu IRC channel (yeah I still use IRC because it’s the best resource for us geeks after Google, maybe even before it). Thank you so much. Even though it took awhile to completely nail this down, it saved me a few hours.

I needed to change a line in file in Linux. The challenge was that the same file existed in many subfolders and I needed to change all of them. Searching on Google was not working out too well as some of the suggestions didn’t work. I could have learned Perl to do it but instead I went on IRC and asked for help like a real man does.

After working with ek, the solution was:

find . -type f -iname “file.name” -exec echo ‘Editing {}’ \; -exec sed -i.bak -e “s|search|replace|” {} \;

For me specifically it was:

find . -type f -iname “configuration.php” -exec echo ‘Editing {}’ \; -exec sed -i.bak -e “s|\$mosConfig_sef = ‘0′|\$mosConfig_sef = ‘1′|” {} \;

The $ had to be escaped. When I ran it without escaping the dollar sign it replaced all instances of ‘0′ with ‘1′ which was bad. It was a good thing the script created backups of the originals before writing the new files. I restored the originals and then ran the line above and all was good.

IRC can be your friend, but remember to be nice on there, don’t ask to ask (Just ask the question) and don’t get upset when the suggestions don’t work. After all, the people on there are just like you and me and usually are not paid for any help they give.