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.
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:
#!/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'
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)