substitution to mulitple files

Hello all,

I am trying to make a script that will apply a substitution to any number of files given on the command line.
Example would be

~/Unix/script/subst car boat myFile1.txt myFile2.txt myFile3.txt

This is the code I have so far but it does not function as needed.

PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
mv $3 $3.bak
sed "s/$1/$2/g" $3.bak > $3
done

Looking for any pointers.

Thanks

PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
mv $FILE $FILE.bak
sed "s/$1/$2/g" $FILE.bak > $FILE
done

You almost had it... You saved the pattern and replacement strings, but instead of using those saved values in your call to sed, you used $1 and $2 after shifting them away... Similarly, you used $3 instead of $FILE. The suggestion from rdrtx1 fixed the last issue, but not the first. Try:

PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
mv $FILE $FILE.bak
sed "s/$PAT/$REPL/g" $FILE.bak > $FILE
done

Note that if there could be hard links to any of your $FILE operands or if the permissions on $FILE aren't the current default for newly created files, you should consider using cp instead of mv when creating your backup files (but you will need to be sure that the current permissions allow you to write the file).

Actually, that is closer to the results I am looking to but they still are not exact.
Example

~/Unix/script/subst "hi guys" "hello everyone" myFile1.txt myFile2.txt myFile3.txt

and I want it to make that replacement in everyfile. my code works for just one file, but I am having some trouble manipulating the loop to make it work for multiple files.

When I created the following three files:
f1:

cat calico
dog dachsund

f2:

cat tabby
dog collie

f3:

cat siamese
dog retriever

and stored the following in a file named [icode]tester[icode]:

#!/bin/ksh
set -xv
PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
        mv $FILE $FILE.bak
        sed "s/$PAT/$REPL/g" $FILE.bak > $FILE
done

and made tester executable by:

chmod +x tester

and executed it with the command line:

tester cat kitty f?

the tracing provided clearly shows that cat appears in the sed search field and kitty appears in the sed replacement field while editting f1, f2, and f3. All of the original files are preserved with the .bak extension and all of the original file names now contain kitty instead of cat.

What wasn't working for you?

What would I do if I am not just using .bak files such as .dat or .foo ?

I don't understand the question. You said you're using the script I provided and it isn't working. If you're saying that you changed .bak to .dat or to .foo in both places where it appears in the script, it shouldn't make any difference. If you're saying you changed:

        mv $FILE $FILE.bak
        sed "s/$PAT/$REPL/g" $FILE.bak > $FILE

to:

        sed "s/$PAT/$REPL/g" $FILE > $FILE

then you have thrown away all of your data and created empty files in their place.

If you're saying that you're running on DOS that only allows a single "." to appear in a filename and moving x.dat to x.dat.bak isn't working; you should have told us up front that even though you're posting in the UNIX and Linux forums, you're not running on a UNIX-, Linux-, or BSD-system and have a much tighter set of constraints not placed on applications using standard utilities in a roughly POSIX conforming environment.

I am in a Unix environment. Basically this is the error I am getting.

+ REPL='U N I X' 
shift
+ shift

for FILE in $*
do
        mv $FILE $FILE.txt
        sed "s/$PAT/$REPL/g" $FILE.txt > $FILE
done
+ FILE=_king
+ mv _king _king.txt
+ sed 's/Unix/U N I X/g' _king.txt
+ FILE=cobra.dat
+ mv cobra.dat cobra.dat.txt
+ sed 's/Unix/U N I X/g' cobra.dat.txt
+ FILE=_ne$wt.foo
+ mv '_ne$wt.foo' '_ne$wt.foo.txt'
+ sed 's/Unix/U N I X/g' '_ne$wt.foo.txt'

I apologize for my careless programming. I was lured in by your example with filenames myFile1.txt , myFile2.txt , and myFile3.txt . Having a <dollar-sign> in a filename is a completely different game. Please try again with this script:

#!/bin/ksh
set -xv
PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
	mv "$FILE" "$FILE.bak"
	sed "s/$PAT/$REPL/g" "$FILE.bak" > "$FILE"
done

I'm assuming that you don't have a shell variable named wt so an unquoted expansion of _ne$wt.foo.txt expanded to _ne.foo.txt giving you unknown file errors (even though you didn't show any error messages in the output of running your script.

The error I get is

Incorrect output on test 33: /home/sub3 'Unix' 'U N I X' '_king cobra.dat' '_ne$wt.foo'

Please show us the code that produced those error messages!