Shell script to replace strings in all files under directory recursively
Linux/Unix, Shell script June 30th, 2007
In one of my project I need to replace some kind of strings in hundreds if file, the code paste here is for specific usage, but it could be easily changed to do something else.
It will scan all *.xml files in current directory and in sub directories, replace the value of two attributes to a big value.
| #!/bin/bash |
| # Get List of Files to Be Replaced |
| file_list=`find . -name ”*.xml” -type f` |
| # Define Target String to Be Replaced & Destination String for Substitution |
| echo ” Replacing *.xml file’s timeout to a big value ” |
| # Perform Substitution |
| for fn in $file_list |
| do |
| if ( test $fn != ./`basename $0` ) |
| then |
| ffnt=”$fn.temp” |
| echo ”Processing $fn …… ” |
| sed ’s/responseReadTimeout=”[0-9][0-9]*”/responseReadTimeout=”999999″/g’ $fn > $ffnt |
| sed ’s/socketReadTimeout=”[0-9][0-9]*”/socketReadTimeout=”999999″/g’ $ffnt > $fn |
| rm $ffnt |
| echo ” Done ” |
| fi |
| done |
Popularity: 15% [?]
About
Leave a Comment