Shell Script to sed with Multiple Files


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell Script to sed with Multiple Files
# 1  
Old 12-06-2013
[Solved] 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 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
Code:
~/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:

Code:
~cs252/bin/scriptAsst.pl

to complete the assignment.


My code -

Code:
#!/bin/sh

p1="$1"
shift
p2="$1"
shift
p3="$1"
shift

for FILE in "$@"
do
        echo $p1
        echo $p2
        echo $p3

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

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



Results -

Code:
sub3 produced incorrect output on test 28: /home/cbliss/UnixCourse/scriptAsst/sub3 '3' '9' '_aardvark.cpp'

I do not know what test 28 inputs or the contents of _aadvark.cpp. We run a given command and it automatically tests our script. If it passes a test we see:
Code:
1...
2...
3... etc.


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):


Old Dominion University - Norfolk, Va USA
Professor Steven Zeil
CS 252 - An Introduction to Unix for Programmers

Last edited by Don Cragun; 12-06-2013 at 12:39 AM.. Reason: Add more CODE tags.
# 2  
Old 12-06-2013
Have you noticed that you're setting $FILE in your while loop but you never refer to it?
Does the example below give you any ideas on how to proceed:
Code:
#!/bin/sh
p1="$1"
shift
p2="$2"
shift

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

# 3  
Old 12-06-2013
Okay, I changed the code to
Code:
#!/bin/sh

p1="$1"
shift
p2="$1"
shift

for file in *
do
      echo $p1
      echo $p2

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

And received the error
Code:
sub3 appears to be treating its first parameter as the name of a file to be edited.

for test 28.

So then I changed my script to -
Code:
#!/bin/sh

p1="$1"
shift
p2="$1"
shift
p3="$1"
shift


for file in *
do
      echo $p1
      echo $p2
      echo $p3

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

Which passes test 28, but fails test 29 and does not address what you were saying.

How would I go about referring to file? Setting $p3=file while inside the loop? The one example in the slides we have is
Code:
for file in *
do
  echo Looking at $file.
  count=`expr $count + 1`
  echo Count is $count.
done

And it uses that snippet repeatedly.

And the failed test output is -
Code:
sub3 produced incorrect output on test 29: /directory/sub3 'e' 'x' '_bongo.dat' '_cat.dat'

I think I see what is wrong here, having 3 argument parameters and it giving four, but how do I account for X number of files?
# 4  
Old 12-06-2013
Did it look like you were getting the correct set of arguments to give to sed when you tried my sample script? (Did you try my sample script before changing it?)

Why did you change:
Code:
for FILE in "$@"

in your original code and in my example to:
Code:
for FILE in *

The shell expands "$@" to a list of the quoted arguments remaining on your command line after the shift commands removed your first two command-line arguments. The shell expands * to a list of files in the current directory.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-06-2013
Using
Code:
for FILE in "$@"

I get the error
Code:
sed can't read : No such file or directory
mv: cannot move `temp.out' to `': No such file or directory


What I have with your recommended changes -
Code:
#!/bin/sh

p1="$1"
shift
p2="$1"
shift
#p3="$1"
#shift

for FILE in "$@"
do
        echo $p1
        echo $p2
        set p3="$file"
        echo $p3

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

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

# 6  
Old 12-06-2013
I repeat: Did you try my sample script before changing it?

Please try the sample script I gave you without changing it. (Note that there is a HUGE difference between $FILE and $file.) Using:
Code:
for FILE in whatever

and referencing $file in the loop won't work.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 12-06-2013
I hadn't even realized that I was using
Code:
FILE

and
Code:
$file

. My apologies. So, this is my code at present:

Code:
#!/bin/sh

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
        #p1="`cat temp`"
        #rm temp
        #echo $p1

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

Running
Code:
~cs252/bin/scriptAsst.pl

This is the output -
Code:
Checking sub3...
28...
29...
30...
31...
sed: can't read _king: No such file or directory
sed: can't read cobra.dat: No such file or directory
mv: target `cobra.dat' is not a directory
32...
33...
sed: can't read _king: No such file or directory
sed: can't read cobra.dat: No such file or directory
mv: target `cobra.dat' is not a directory

sub3 produced incorrect output on test 33: /home/cbliss/UnixCourse/scriptAsst/sub3 'Unix' 'U N I X' '_king cobra.dat' '_ne$wt.foo'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help getting a UNIX shell script to look at multiple files.

Hey everyone! I made a shell script that would go through a file and replace any phrase or letter with another phrase or letter. Helps update variable names or values. The following code is this: #!/bin/sh Word1="$1" Replace1="$2" File1="$3" arg=$( echo "$Word1" | sed 's:\:\\&:g' )... (3 Replies)
Discussion started by: rebmonk
3 Replies

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

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

4. Shell Programming and Scripting

shell script to ftp multiple files

Hi, i use the below script to send a single file to remote server from linux. ftp -nvi <<!EOF open $Host_name user $USER_ID $PWD binary mput $file_name quit !EOF (where i... (2 Replies)
Discussion started by: pradebban
2 Replies

5. Shell Programming and Scripting

Renaming multiple files with a shell script

Hey guys, I'm really new to UNIX and shell scripting in general. For my internship I need to rename a bunch of files. Specifically, I need to change the first letter of each of the files to lowercase and I have to change the endings so they all basically look like "file_cone.jpg". I know I... (4 Replies)
Discussion started by: jjzieve
4 Replies

6. Shell Programming and Scripting

Need shell script for ftpying multiple files

Hi All, I am a beginner for shell programming. I have a requirement to ftp multiple files. Here are the details. I have around thiry files in one directory, I want a shell script which selects 5 files at a time and does ftp them to another host . After the transfer for first files is... (0 Replies)
Discussion started by: srikanthn
0 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 script to join multiple files

I am a new to Linux and try to write a script to join three multiple files. For example, there are three files file1 # comment a Kevin b Vin c Sam file 2 # comment a 10 b 20 c 40 file 3 # comment a blue b yellow (7 Replies)
Discussion started by: bonosungho
7 Replies

9. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies

10. Shell Programming and Scripting

how to send multiple files from the shell script

hi, how to send multiple files from the shell script eg : i have /home/adm/file1 /home/adm/file2 /home/adm/cfg how can i attach these files in the mail ? (1 Reply)
Discussion started by: mail2sant
1 Replies
Login or Register to Ask a Question