Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shell while loop broken after using AWK

Status
Not open for further replies.

byh

Technical User
Mar 11, 2007
2
US
#!/bin/sh

while read x1 x2 x3 x4 x5 x6 x7 x8 nc
do

eval "$(awk -v string="$nc" 'BEGIN {num=split(string,a,"/")} {len=length(string)} {lenx=length(a[num])} {printf "cdir=%s\n", substr(string,1,len-lenx-1)} END {printf "fnm=%s\n", a[num]}' )"

echo 'nc='$nc' cdir='$cdir' fnm='$fnm
done < /ptmp/wx52bh/fileMir.loc

Since the text in file fileMir.loc is more than one line, I expect to have more than one output.
However, the program stops after one output.

Any solutions or problems?

Thanks
 
Your awk program read its standard input, ie fileMir.loc !
you may try this instead:
eval "$(awk -v string='$nc' 'BEGIN{num=split(string,a,"/");len=length(string);lenx=length(a[num]);printf "cdir=%s\nfnm=%s\n", substr(string,1,len-lenx-1), a[num]}')"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Dear PH,

Thanks to your reply, the program works much better. The loop continues as I wanted. But the out puts are not quite right:

The line read is "/u/wx52bh/tmp/grads/get_time.gsf"

I wish to get two line outputs
cdir=/u/wx52bh/tmp/grads
fnm =get_time.gsf

But what I got is
cdir=
fnm =/u/wx52bh/tmp/grads/get_time.gsf

Which is very strange.
Any further advice is expected and will be very much appreciated.
 
In fact you don't need awk at all:
while read x1 x2 x3 x4 x5 x6 x7 x8 nc
do
cdir=$(dirname "$nc")
fnm=$(basename "$nc")
echo 'nc='$nc' cdir='$cdir' fnm='$fnm
done < /ptmp/wx52bh/fileMir.loc

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top