bash one liner to change/remove test pattern from config file or text file.
Problem:
I had a directory with a lot of config files in the ini file format, i.e. name and value separated by equals. Some/all of these contained some directory names and other values which had to be replaced. But there were other place where I should not not replace them, if there was no exact match. I also needed to have a backup copy of the file that I was modifying. Since the number of files that I needed to change was multiple so was quite difficult to do it without using some script.
Solution:
for i in $(grep -l ’/opt/amitag’ *) ;
do sed ’s@/opt/amitag/@@’ $i >${i}.new;
mv $i{,.bak};
mv $i{.new,} ;
done
I was doing it in a box where sed did not have the -i option and hence I had to write sed output to different file, and then move the files back and forth. You can change the string ”/opt/amitag” in the above commands to anything you like 🙂
Related Articles:
- 2010/06/24 Script to add all the partitions to the fstab.
- 2010/05/19 bash script to change icon theme to check out all the installed themes (personal)
- 2010/05/07 bash script to periodically change the cursor theme.
- 2010/06/25 find duplicate entry in a list in bash with sed
- 2010/06/25 remove/replace text/path in config file.
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.