Editing script for multiple files trouble

May 4, 2010 at 10:56:24
Specs: Windows Vista, amd 6400/4gb
Hey everybody, I have a script for making a string substitution in a file. I am trying to modify it in order to make the same modifcation to multiple files. here is what I have so far.

set p1="$1"
set p2="$1"
foreach x ($*)
if ( { grep -w -c "$p1" $x } ) then
mv $x $x.bak
sed "s/$p1/$p2/g" $x.bak > $x

An example would be
~/Unix/script/subst "hi guys" "hello everyone" myFile1.txt myFile2.txt

which would make the same substitution in each file, and this should work for any number of files, with any substitution. the script works just fine for one file but I am haiving touble making it work for more than one currently.
any help would be great. Thanks in advance!

See More: Editing script for multiple files trouble

May 6, 2010 at 18:07:51
would just like to add everything my script is supposed to do with its parameters.

1.the string to be replaced
2.the string with which to replace it
3.the name of the file in which to make the substitution
4.performs the substitution and backup file creation when the target string occurs somewhere in the file, but leaves the file completely unchanged (and does not create the .bak file) if the string being replaced is nowhere in the file5. will apply a substitution to any number of files given on the command line

Report •
Related Solutions

Ask Question