Script for replacing string in file
This is the script to replace a particualr pattern in the files. The script works recursively. This is actually combination of the two scripts. as shown below. Just copy the above below in a file named “replaceInFile” #!/bin/sh #Script for replacing patterns in files numPar=$# pattern=$1 text=$2 if [ $numPar -gt 2 ] then chmod 777 $3 printf "Replacing $1 by $2 in $3 ..." sed -e "s/$pattern/$text/g" $3 1>> $3.new mv $3 $3.bak mv $3.new $3 echo "done." else printf "repStr: invalid parameter count!\n" fi This is the script which will actually perform the replacement. It uses the “sed” command to perform the actual substitution. Now as we want the replacement to be done recursively in a folder we will use another script which will give the filenames one by one to this script. Copy the following code in the file named replaceAll #!/bin/sh find . -name "*.cpp" -exec ./replaceInFile "<>" " <>" {} \; The above script will call the “replaceInFile” script for each “.cpp” of the file in the directory. The patterns to be searched and replaced should be mentioned in the breackets. Now copy both the scripts in the directory where the replacement is to be done and just rin the replaceAll script. ( The script generates “.bak” files as backup ) |
0 Responses to Script for replacing string in file
Something to say?