remove/replace text/path in config file.

2010-06-25 134 words 1 min read

Lot of times I end up changing a particular text in config file to some other text. And when I have to do it for multiple files, all files having the text in multiple places, I end up opening the files in vim and then doing a globlal replace. But this is not efficient so I cam up with some one liners to do this for me. For removing any occurance of text in all files in the directory :

for i in $(grep -l ’text-to-remove’  *) ; do sed ’s@text-to-remove@@’ $i >${i}.new; mv $i{,.bak}; mv $i{.new,}; done

And if you want to replace the text then the same command with little change in sed command:

for i in $(grep -l ’text-to-replace’ *) ; do sed ’s@text-to-replace@new-text@’ $i >${i}.new; mv $i{,.bak}; mv $i{.new,}; done


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it