Help with Shell Scripts Using sed in multiple files.

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with Shell Scripts Using sed in multiple files.
# 8  
Old 08-09-2012
Show exactly what you're doing, word for word, letter for letter, keystroke for keystroke, and copy/paste the error messages literally.
# 9  
Old 08-09-2012
I typed in
Code:
#!/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.

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

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Corona688; 08-09-2012 at 09:00 PM..
# 10  
Old 08-09-2012
Without knowing what that script expects it's difficult to help.

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

Code:
shift 2 # get rid of first two parameters

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

# 11  
Old 08-09-2012
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.
# 12  
Old 08-09-2012
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.
# 13  
Old 08-09-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Shell Script to sed with Multiple Files

I am not sure what I am doing wrong here. The code should work fine. I have been making small changes insuring that each new bit works. Now running my sed through multiple files I am getting incorrect output. Any help and instruction would be greatly appreciated. The problem - Generalize... (10 Replies)
Discussion started by: Lycopene
10 Replies

2. Shell Programming and Scripting

Multiple shell scripts executed in one script

Hi every one, i am new to shell script. my people given a task to write a shell script that should execute number of shell scripts in that. in that, if any shell script is failed to execute, we have to run the main script again, but the script should start execute from the failed script only.. it... (6 Replies)
Discussion started by: Madhu Siddula
6 Replies

3. Shell Programming and Scripting

Multiple shell scripts executed in one script

Hi every one, i am new to shell script. my people given a task to write a shell script that should execute number of shell scripts in that. in that, if any shell script is failed to execute, we have to run the main script again, but the script should start execute from the failed script only.. it... (1 Reply)
Discussion started by: Madhu Siddula
1 Replies

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

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

6. UNIX for Dummies Questions & Answers

Use of grep with multiple parameters in shell scripts

I am learning how to write shell scripts and have come across an issue. I'm trying to write a script that looks for a directory called public_html, and if it finds one, to print the number of lines that contain applet tags (containing '<applet') in all files that end in either .html or .htm that... (7 Replies)
Discussion started by: feverdream
7 Replies

7. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

8. Shell Programming and Scripting

shell scripting-processing multiple scripts

I have four scripts to run. My 1st script will make script2 and script3 to run. I am setting a cron job for this script1 to run continuously. This script1 will check for 2 text files and based on the existance of those text files it will initiate the script2 and script3. Now my doubt is that... (2 Replies)
Discussion started by: RSC1985
2 Replies

9. Shell Programming and Scripting

Manipulating a variable across multiple shell scripts

I have a shell script similar to: #!/bin/sh a=1 source a1.sh -- Modifies a source a2.sh -- Modifies a echo "After execution, value of a is $a" What i need is a1.sh script modify the same variable a and same with a2.sh. Now the echo "After execution, value of a is $a" should print the... (1 Reply)
Discussion started by: indianjassi
1 Replies

10. Shell Programming and Scripting

How to make the same change in multiple shell scripts?

I would like to make the same change in multiple shell script files and would like to know if anyone can be of some help? I would appreciate it. (4 Replies)
Discussion started by: rdakhan
4 Replies
Login or Register to Ask a Question