Asst: Using Shell Scripts with sed

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Asst: Using Shell Scripts with sed
# 1  
Old 12-07-2013
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.


2. Relevant commands, code, scripts, algorithms:

Unix library

3. The attempts at a solution (include all code and scripts):
my code is:

Code:
#!/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).
# 2  
Old 12-07-2013
You store something in the variable plain but then do not use it?!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

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... (16 Replies)
Discussion started by: raymondbn
16 Replies

2. Shell Programming and Scripting

Issue with SUNOS running sed scripts

Hi I probably dont have GNU extended sed in my SUNOS . and its creating lot of problems ex: a simple sed command like this is not working sed '/WORD/ a\ sample text line 1 \ sample text line 1 ' filename sed: command garbled: /WORD/ a I took precaution to have a new line after... (11 Replies)
Discussion started by: vash
11 Replies

3. UNIX for Dummies Questions & Answers

Rename scripts using xargs/sed

Morning all I've got loads of scripts but the names are too long! I've stuck the list in a flat file (names) and I'm trying to read that in line by line and create the new names (in to directory new) from the list. It looks like this: xargs -n1 -I{} <names cat {} | sed... (1 Reply)
Discussion started by: Grueben
1 Replies

4. Homework & Coursework Questions

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... (12 Replies)
Discussion started by: Johnny2518
12 Replies

5. Shell Programming and Scripting

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. 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,... (1 Reply)
Discussion started by: Johnny2518
1 Replies

6. Shell Programming and Scripting

Making use of multiple cores for running sed and awk scripts

Hi All, After reading that the sort command in Linux can be made to use many processor cores just by using a simple script which I found on the internet, I was wondering if I can use similar techniques for programs like the awk and sed? #!/bin/bash # Usage: psort filename <chunksize>... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

7. Shell Programming and Scripting

sed scripts are not playing nicely!

I am having a lot of trouble, wasting a lot of time trying to get sed script files to run without problem. I have a list of scripts I want to run on a text file to turn it into a viewable html file for my mobile phone. Each of the single scripts when run in order/sequence one after another on the... (4 Replies)
Discussion started by: naphelge
4 Replies

8. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

9. Shell Programming and Scripting

number sign(#) in sed scripts

Hi. In a simple sed script i have number signs - #. But i can't find any description on their functionality. One thing that i find is that # in sed scripts can mean comment if it is placed in the beginning of the line. I thought may be this undocumented(for me) behavior refers to regular... (1 Reply)
Discussion started by: kukuruku
1 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question