Asst: Shell Script to sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    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.
Give the command:

~cs252/bin/scriptAsst.pl

to complete the assignment.

  1. Relevant commands, code, scripts, algorithms:
    Using pico for scripting in putty

  2. The attempts at a solution (include all code and scripts):
    My attempt at the Assignment:

#!/bin/sh
p1="$1"
shift
p2="$1"
shift

for FILE in "$@"
do 
echo $p1
echo $p2
echo "$p1" | sed -e 's/\([*.[^$]\)/\\\1/g' > temp
p1="`cat temp`"
rm temp 
echo $p1

sed "s/$p1/$p2/g" "$FILE > temp.out
mv temp.out "$FILE"
done

I noticed there was a previous thread about this, but with my code its not working and i dont know why.

The error im recieving is

sub3 produced incorrect output on test 37: /home/bnaranjo/UnixCourse/scriptAsst/sub3 'l*' 'L' '_cat.dog.dat' '/home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp'

I appreciate any help and anybodys time for looking at it, its my last assignment
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Old Dominion University, Norfolk(VA), USA, Zeil, csc 252

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

There seems to be a double quote missing with the second sed command.

Oh that mustve been a typing error i did, the code includes the parenthesis in my code, im sorry. Thank you for taking a look, any other ideas you could give me?

---------- Post updated at 12:19 AM ---------- Previous update was at 12:17 AM ----------

Oh that mustve been a typing error, i include the parenthesis in my code, im sorry. Thank you for taking a look, any other ideas ?

I meant a double quote: " , around "$FILE" not a parenthesis () . What is not working? What is producing this error message?

Yeah the double quotes you mentioned, thats whats in my original code.But im not sure what the error is, my guess maybe sed isnt working well for the last file being changed, it works fine up from 1 to 36, then at 37 it gives me trouble. It says sub3 produced incorrect output on test 37

the full error message is up in my original message up top

Yes but the error message is not produced by the shell interpreting the script, or a Unix utility but by something else. We have no way of knowing what that is. What could be is that the variables in the sed statement contain a forward slash, which would mess up the command. Then you could use another character instead of the / . For example:

sed "s#$p1#$p2#g"

oh yeah, i tried the sed command, still no luck, gave me the same results, i really appreciate you being patient with me and helping, i just dont know how else i could go about this, do you know how i could debug that file?

On the second line you could add a

set -x

statement. It may tell you some more...

sh -x sub3 'l*' 'L' '_cat.dog.dat' 'home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp'
+ p1=l*
+ shift
+ p2=L
+ shift
+ echo l*
l*
+ echo L
L
+ echo l*
+ sed -e s/\([*.[^$]]\)/\\\1/g
+ cat temp
+ p1=l*
+ rm temp
+ echo l*
l*
+ sed s/l*/L/g _cat.dog.dat
+ mv temp.out _cat.dog.dat
+ echo l*
l*
+ echo L
L
+ echo l*
+ sed -e s/\([*.[^$]]\)/\\\1/g
+ cat temp
+ p1=l*
+ rm temp
+ echo l*
l*
+ sed s/l*/L/g home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp
sed: can't read home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp: No such file or directory
+ mv temp.out home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp
mv: cannot move `temp.out' to `home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp': No such file or directory

Its not grabbing the /_hippo.cpp file on this test, how can i make it able to do that?

Do you mean to use a relative path from your current directory and if so can your ls -l the file? Or do you mean to use

/home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp

Yeah i need to use

home/bnaranjo/UnixCourse/scriptAsst ../_hippo.cpp

, its part of the script testing from what i now, thank you srutinizer

---------- Post updated at 11:30 PM ---------- Previous update was at 07:18 PM ----------

How would i use ls -l in the file?

I mean on the command prompt, go to the directory where you call the script and then issue the command

ls -l home/bnaranjo/UnixCourse/scriptAsst/../_hippo.cpp

Where does the home/bnaranjo/ come from?
From your teacher? Maybe it should be /home/bnaranjo/ ?
Do a cd / (where home and /home are the same) and run your script from there!

#!/bin/sh
set -x
A="$1"
shift
B="$1"
shift

for FILE in "$@"
do
        echo $A
        echo $B
        echo "Process $FILE using $A and $B..."

        #echo "$A" | sed -e 's/\([*.[^$]\)/\\\1/g' > target
        #A="`cat target`"
        #rm target
        #echo $A

        sed "s/$p1/$p2/g" "$FILE" > target.out
        mv target.out "$FILE"
done

I got this but get an error.

37...
+ p1=l*
+ shift
+ p2=L
+ shift
+ echo l*
+ echo L
+ echo Process _cat.dog.dat using l* and L...
+ sed s/l*/L/g _cat.dog.dat
+ mv temp.out _cat.dog.dat
+ echo l*
+ echo L
+ echo Process /home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp using l* and L...
+ sed s/l*/L/g /home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp
+ mv temp.out /home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp

sub3 produced incorrect output on test 37: /home/sofestafont/UnixCourse/scriptAsst/sub3 'l*' 'L' '_cat.dog.dat' '/home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp'

Which looks like the regular expression got converted to plain text properly.

The conversion from l* to l\* would happen in the code that you commented out.
Where
A="`cat target`" is problematic, should be a simple read A < target

Without the quotes I get the same error message. Looking through the -x log it shows lots of blank spaces which my sed handles fine. I assume it is that asterik not converting to plain text with my sed command that is messing it up. I've been messing around some more with the quotations because I figure that is where I messed up. I read that * has special characteristics while in double quotes so maybe that is it?

---------- Post updated at 06:03 PM ---------- Previous update was at 04:55 PM ----------

37...
+ p1=l*
+ shift
+ p2=L
+ shift
+ echo l*
+ echo L
+ echo Process _cat.dog.dat using l* and L...
+ + sedecho l*
 -e s/\([*.[^$]\)/\\\1/g
+ read l*
/home/sofestafont/UnixCourse/scriptAsst/sub3: 15: read: l*: bad variable name
+ rm temp
+ echo p1 = l*
+ sed s/l*/L/g _cat.dog.dat
+ mv temp.out _cat.dog.dat
+ echo l*
+ echo L
+ echo Process /home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp using l* and L...
+ + sed -eecho l*
 s/\([*.[^$]\)/\\\1/g
+ read l*
/home/sofestafont/UnixCourse/scriptAsst/sub3: 15: read: l*: bad variable name
+ rm temp
+ echo p1 = l*
+ sed s/l*/L/g /home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp
+ mv temp.out /home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp

sub3 produced incorrect output on test 37: /home/sofestafont/UnixCourse/scriptAsst/sub3 'l*' 'L' '_cat.dog.dat' '/home/sofestafont/UnixCourse/scriptAsst/../_hippo.cpp'

I tried changing the quotations around the parameters in the last sed value all to no avail. I don't understand, why it says l* is getting passed but the output is wrong. I've incorporated the read command.

#!/bin/sh
set -x
p1=$1
shift
p2=$1
shift

for FILE in "$@"
do
        echo $p1
        echo $p2
        echo "Process $FILE using $p1 and $p2..."

        echo "$p1" | sed -e 's/\([*.[^$]\)/\\\1/g' > temp
        read $p1 < temp
        rm temp
        echo "p1 =" $p1

        sed "s/$p1/$p2/g" "$FILE" > temp.out
        mv temp.out "$FILE"
done

~/UnixCourse/scriptAsst/sub3 *l L myFile1.txt myFile2.txt myFile3.txt
Is the input that is messing me up.

read p1 < temp

no $
It's an assignent to p1 not a reference of $p1