remove/replace text/path in config file.

2010-06-25 1 min read bash

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

comments powered by Disqus