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
Related Articles:
- 2010/03/16 Quick tip to change parameters in different files
- 2010/03/08 create SQL-statements from textfile with awk
- 2010/01/21 sed tutorial and help
- 2010/01/07 shell — one liner to selectively change case
- 2009/12/18 Display a block of text with delineated by a start pattern and an end pattern
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.