Asst: Using Shell Scripts with 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:

One problem with the use of sed in simple text substitutions is that sed will intepret some characters as regular expression operators. For example, we might try to change just the punctuation in our earlier example:
echo Hello world. > myFile.txt
~/UnixCourse/scriptAsst/sub1 . ! myFile.txt
cat myFile.txt

and are likely to find
!!!!!!!!!!!!

in myFile.txt instead of the desired "Hello world!".
Create a new script, sub2, taking the three parameters, that treats the string to be replaced as plain text instead of as a regular expression.
The Three parameters :
1.)the string to be replaced
2.)the string with which to replace it
3.)the name of the file in which to make the substitution

Hint 1: Put the target string (the one to be replaced) into a shell variable (e.g., $target). Then use a sed command to rewrite any of the "special" regular expression characters

[ ] ^ * + . $ \ -

into "safe" forms of those same characters by adding backslashes in front (e.g., \) or surrounding them by [ ] (e.g., []). Store that rewritten version of the target string in in a second shell variable (e.g., $newTarget). Then issue the actual command to perform the substitutions on the file.

Hint 2: For technical reasons, this task is probably easier accomplished in /bin/sh than in /bin/csh. If you insist upon using csh to run your script, you might need to solve this by writing your sed commands into a separate temparary file and using the -f option of sed to run commands from that file.

Again, when you think you have this ready, give the command:

~cs252/bin/scriptAsst.pl

to check your scripts so far.

  1. Relevant commands, code, scripts, algorithms:

Unix library

  1. The attempts at a solution (include all code and scripts):
    my code is:
#!/bin/sh

echo "$1" , "$2", "$3"

plain=$(echo "$1" | sed "s:[]\[\^\$\.\*\/]:\\&:g")

sed -e "s/$1/$2/g" "$3" >a.out

mv "a.out" "$3"

It seems like "*" is not passing through as a parameter?

error messase:

"sub2 produced incorrect output on test 19: /home/bnaranjo/UnixCourse/scriptAsst/sub2 'l*' 'L' '_king cobra.dat'

Any help is appreciated, thank you!
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Old Dominion University, Virginia Beach(VA), USA, Zeil, cs 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).

You store something in the variable plain but then do not use it?!