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).
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.
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.
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.