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.
# 1  
Old 08-09-2012
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

Code:
!/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?
Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.
# 2  
Old 08-09-2012
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-09-2012
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

Code:
#!/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


Last edited by Corona688; 08-09-2012 at 07:24 PM..
# 4  
Old 08-09-2012
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

# 5  
Old 08-09-2012
Help

Quote:
Originally Posted by Corona688
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

When i try that, it still says that it produced incorrect output. I can not find the error that is happenengSmilieSmilie
# 6  
Old 08-09-2012
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.
# 7  
Old 08-09-2012
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:
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