Computing.Net > Forums > Unix > awk/sed help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk/sed help

Reply to Message Icon

Name: Vinay
Date: November 6, 2004 at 07:57:03 Pacific
OS: HPUnix
CPU/Ram: 512
Comment:

Hi guys,
Iam a beginner in awk/sed and need a help on awk/sed. This is my requirement. I have a text file with following entry
xx yy zz.txt
aa bb cc.txt
ee ff gg.txt

Iam looking for a program which will go through the text and do the following
cp /xx/yy/zz.txt $HOME
cp /aa/bb/cc.txt $HOME
cp /ee/ff/gg.txt $HOME

Thanks in advance,
Yaniv




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 6, 2004 at 10:20:53 Pacific
Reply:

Yaniv:

You'll probably get more help if you at least post something. I'll do 2 of the 3 for you:

sed -e 's/ /\//g' -e 's/^/cp /g' datafile

There's two sed things going on:

1) the first replaces spaces with a slash. notice because / is a metacharacter that you have to escape it with \

2) the second is a regular expression that inserts 'cp ' at the beginning of the line ^.

Regards,


Nails


0

Response Number 2
Name: Jim Boothe
Date: November 6, 2004 at 11:01:25 Pacific
Reply:

Using Nail's approach of using sed to generate a copy script, then execute that script, is a good way to go.  Another approach would be to use awk which can do the cp commands directly.

Use the system function to execute OS commands from within awk. You cannot reference $HOME directly within the awk program, but there are several ways of getting an environment variable into awk.

This first way simply places the $HOME variable outside of the single-quoted awk program so that the shell interprets it before awk executes.

awk '{
system("cp /"$1"/"$2"/"$3" '$HOME'")
}' textfile

This second way uses awk's -v option to feed the variable into awk on the command line:

awk -v myhome=$HOME '{
system("cp /"$1"/"$2"/"$3" "myhome)
}' textfile

There are other ways, including pulling the variable from the environment using awks ENVIRON function.

My preferred approach would be to use neither sed nor awk, but instead just a simple shell loop, which I will post shortly.


0

Response Number 3
Name: Jim Boothe
Date: November 6, 2004 at 11:09:40 Pacific
Reply:

while read dir1 dir2 fname remainder
do
fullname=/$dir1/$dir2/$fname
if test -r /$fullname ; then
   cp $fullname $HOME
else
   echo "$fullname not accessible"
fi
done < textfile


0

Response Number 4
Name: Vinay
Date: November 6, 2004 at 12:31:43 Pacific
Reply:

All,

thanks for the help..Jim, I tried the script you gave last. does it work ony for the first line? for me when i tried executing, it copied only the file in the first line. How to make t work for all line? also if you could tell me what the line
"if test -r" does that will be great...
thanks a bunch..

Yaniv


0

Response Number 5
Name: Jim Boothe
Date: November 6, 2004 at 12:46:21 Pacific
Reply:

That script will process each line in textfile.

The test -r checks to see if the file exists AND is readable by you. For each line in textfile, it will do either a copy or an echo.


0

Related Posts

See More



Response Number 6
Name: Vinay
Date: November 6, 2004 at 16:16:15 Pacific
Reply:

Jim,
I was testing with two files and hence i thought it was working on only one line. I tried with 5 lines in the txt file and strangely for me it copied all except the last line. It doesnt give any error for the last line, just wont process at all.
Any thoughts what could be the reason.
Appreciate the help,
thanks



0

Response Number 7
Name: Jim Boothe
Date: November 7, 2004 at 05:29:08 Pacific
Reply:

You would get that result if the last line is not properly terminated with a newline character.

If you do cat textfile you would see that your shell prompt remains at the end of the 5th line instead of on the line below. If you do wc -l textfile it will return a line count of 4.

If you find that to be the case, open the file with vi, make a change then change it back then save the file.


0

Response Number 8
Name: Vinay
Date: November 7, 2004 at 06:21:58 Pacific
Reply:

Wow..That worked
Thanks Jim, for the help...


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk/sed help

Different results from awk, sed, tr www.computing.net/answers/unix/different-results-from-awk-sed-tr/7711.html

Awk/Sed Question www.computing.net/answers/unix/awksed-question/7889.html

Help with awk/sed www.computing.net/answers/unix/help-with-awksed/6548.html