String Substitution Question

When I run the script I pass in 2 expressions (ex. replace.ksh new old)

I want the script to go line by line for a given file in a given directory and replace the word new with old. Of course in my line where I have the awk statement it is replacing the 2nd word with 1st instead of new with old. The only caveat to this is that this script will be replacing directory structures. We want to go from a development dir to a production dir and make the change to every file within a directory.

Any help with this would be very much appreciated.

for FILE in `ls`
do
while read LINE
do
echo $LINE | grep -q $1
if [ $? -eq 0 ]
then
# print $1 ${1} $2 ${2}
# val=$1 chval=$2
echo $LINE | awk '{sub($1,$2);print}' >> ${FILE}_new
else
echo $LINE >> ${FILE}_new
fi
done < $FILE
# mv TMP_00 $FILE
# rm TMP_00
done

I think you could use sed for this:

for FILE in `ls`
do
 sed 's/'$2'/'$1'/g' < $FILE > TMP_00
 mv TMP_00 $FILE
done

Thanks oombera, but the code did not work. It cycled through the directory, but it did not make any changes.

Did you call it using scriptName pass1 pass2?

Does pass2 actually exist in any of the files, since that's what you're searching for?

How do you know it even cycled through the files if no changes were made? Try throwing an echo $FILE on a new line after the "move" command and see if it lists each file in the directory.

That code works for me.