Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. What i have so far is

!/bin/sh
File1="$3"
File2="$4"
File3="$5"
testNum="$File1"

while test "$testNum" <= "$File3";
do
echo "$File3"

 A="$1"
echo $A
 B="$2"
 C="$3"
echo "$A" | sed -e 's/\([*.[^$]\)/\\\1/g' > temporary
 A="`cat 'temporary'`"
rm temporary
echo "$A"

sed "s/$A/$B/g" "$C" > myFile.txt.updated
mv myFile.txt.updated "$C"
cat "$C"

done

When ever i try to execute it it says that it cannot =: open such file

Can someone help?

What are $3, $4, $5? Give an example of how you use your program.

You forgot the # in front of the first line. Needs to be #!/bin/sh

You don't need to use the 'test' executable here -- just put your expression in brackets. while [ "$testNum" <= "$File3" ] That is the actual cause of the error here -- without the [ ] , it assumes the < is a file redirection and tries to read from a file named "=". I'm guessing you're doing string comparison there. If you're not, please explain, because it looks a bit odd.

There's quite a few inefficiencies in your code, if you explain what you're trying to do in detail we can show you better ways.

1 Like

I use the program to change an occurance of a word throughout different files that are being tested. At first i had to Create a new script, sub2, taking the same three parameters (replacing an occurance of a word in a shell script), that treats the string to be replaced as plain text instead of as a regular expression. I did that and now I have Generalize my sub2 script, producing a new script sub3 that will apply a substitution to any number of files given on the command line. For example
~/UnixCourse/scriptAsst/sub3 foo bar myFile1.txt myFile2.txt
myFile3.txt

should apply the same substitution throughout each of the three files named there.
I took that part out and now i have

#!/bin/sh
File1="$3"
File2="$4"
File3="$5"
testNum="$File1"

for file in ~/*
do
echo $testNum

 A="$1"
echo $A
 B="$2"
 C="$3"
echo "$A" | sed -e 's/\([*.[^$]\)/\\\1/g' > temporary
 A="`cat 'temporary'`"
rm temporary
echo "$A"

sed "s/$A/$B/g" "$C" > myFile.txt.updated
mv myFile.txt.updated "$C"
cat "$C"

done
#!/bin/sh

# Save first two parameters
A="$1" 
B="$2"

shift 2 # Get rid of first two parameters

while [ "$#" -gt 0 ]
do
        sed "s/$A/$B/g" $1 > $1.new
        shift
done

When i try that, it still says that it produced incorrect output. I can not find the error that is happeneng:wall::wall:

What says it produced incorrect output?

Show exactly what you're doing, word for word, letter for letter, keystroke for keystroke, and copy/paste the error messages literally.

Sorry for the confusion. I entered in the code you showed, and ran a program that takes that code and uses it on different files. While in the file it finds a certain expression and replaces it with another. When i ran the program it said that sub3 (the program code i am trying to write) produced incorrect output on test 28:

Show exactly what you're doing, word for word, letter for letter, keystroke for keystroke, and copy/paste the error messages literally.

I typed in

#!/bin/sh

# Save first two parameters
A="$1" 
B="$2"

shift 2 # Get rid of first two parameters

while [ "$#" -gt 0 ]
do
        sed "s/$A/$B/g" $1 > $1.new
        shift
done

and saved it.
I then ran the program ~cs252/bin/scriptAsst.pl
and it started running the tests. When it got to test 28 it says sub3 produced incorrect output on test 28: /Unix/Unix/sub3 '3' '9' '_aardvark.cpp'

Im guessing that it was trying to replace all occurances of '3' with '9' but the output didn't come out correct.

---------- Post updated at 07:21 PM ---------- Previous update was at 06:57 PM ----------

Basically what i need is a for loop to allow it to run through as files as it wants to. Using the following code.

#!/bin/sh
File1="$3"
File2="$4"
File3="$5"
testNum="$File1"

for x in $*
do
echo $x

 A="$1"
echo $A
 B="$2"
 C="$3"
echo "$A" | sed -e 's/\([*.[^$]\)/\\\1/g' > temporary
 A="`cat 'temporary'`"
rm temporary
echo "$A"

sed "s/$A/$B/g" "$C" > myFile.txt.updated
mv myFile.txt.updated "$C"
cat "$C"

done

I appreciate your patience with me very much

Without knowing what that script expects it's difficult to help.

A for loop to run through your files would be as simple as:

shift 2 # get rid of first two parameters

for FILE in "$@"
do
        ...
done

This should help as to what it expects. It wants us to Generalize your sub2 script, producing a new script sub3 that will apply a substitution to any number of files given on the command line.
For example
~/UnixCourse/scriptAsst/sub3 foo bar myFile1.txt myFile2.txt
myFile3.txt

should apply the same substitution throughout each of the three files named there.

That's what the first script I gave you did, except it generates .out files instead of putting it in the original file.

Read the code, understand it, and you will know how to complete it.

I'm trying, the only problem is i don't understand this section that i am doing. I entered the first script you gave me and it said that it did not work because it could not find the file.