Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txtIam 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 $HOMEThanks in advance,
Yaniv

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

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'")
}' textfileThis 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)
}' textfileThere 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.

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

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

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.

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

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.

![]() |
![]() |
![]() |

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