You should use the sed command for this kind of substitution:
An example that will LIST the files:
Code:
for FILE in `grep -Rl <string> *`
do echo $FILE
sed -e s/<string>//g $FILE
echo ""
done
The -Rl says to do recursive and list only the files (not the lines) that match.
The for loop uses each file found. (Of course you put your string rather than "<string>" in the line that starts with sed.
The sed line says to substitute what is between the first two slashes with what is between the last two slashes - since there is nothing between the last two slashes it essentially erases the original string. The g at the end tells it to do this globally (otherwise it would only do first occurrence).
WARNING: BEFORE PROCEEDING *TEST* ON NON-CRITICAL DIRECTORY/FILES
The below has NOT been tested by me so if you run it you do so
at your own risk-it is provided for EXAMPLE purpose only.
The above does not change the actual files themselves - it just shows you what would be changed. To modify the files you'd have to expand the script somewhat:
Code:
for FILE in `grep -Rl <string> *`
do echo $FILE
sed -e s/<string>//g $FILE >${FILE}.new
cp ${FILE}.new $FILE
rm ${FILE}.new
echo ""
done
The sed line was change to redirect output into a new file suffixed as .new. The next line copies the revised file over the original and then the rm removes the file.new. (The cp is done so the file maintains the permissions it had before you started.)