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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nawk 2

Status
Not open for further replies.

buccaneers

Programmer
Jun 3, 2004
28
US
Hello Gurus,

One question if i may ask.

In a flat file the record is as follows.

ab c ~~ efg ~~ hij ~~ klm

i assigning it to array in ksh in the format specified below
ab c
efg
hij
klm

i am able to achieve this by using following

tbldata[0]=`nawk '{ FS = "\~\~" } ; { print $1 }' flnme.out`
tbldata[1]=`nawk '{ FS = "\~\~" } ; { print $2 }' flnme.out`
tbldata[2]=`nawk '{ FS = "\~\~" } ; { print $3 }' flnme.out`
tbldata[3]=`nawk '{ FS = "\~\~" } ; { print $4 }' flnme.out`

What i am trying to do is to write a for loop where i can put the value of variable instead of $1,$2 etc.

below is the for loop i wrote but it spooled out an error and mentioned below.

t=4
s=0

while [ $s -lt $t ]
do
u=`expr ${s} + 1`
echo $u
tbldata[$s]=`nawk '{ FS = "\~\~" } ; { print $u }' flnme.out`
s=`expr ${s} + 1`
done

this reports the error as
1
nawk: illegal field $()
input record number 1, file flnme.out
source line number 1
2
nawk: illegal field $()
input record number 1, file flnme.out
source line number 1
3
nawk: illegal field $()
input record number 1, file flnme.out
source line number 1
4
nawk: illegal field $()
input record number 1, file flnme.out
source line number 1


Would appreciate if any one can help me in this ?

TIA






 
Try this (untested)

tbldata[$s]=`nawk -v k=$u 'BEGIN{ FS = "\~\~" } { print $k }' flnme.out`

Also, the backslashes are probably not necessay, try it without them

CaKiwi
 
Replace this:
tbldata[$s]=`nawk '{ FS = "\~\~" } ; { print $u }' flnme.out`
By this:
tbldata[$s]=`nawk '{ FS = "\~\~" } ; { print $'$u' }' flnme.out`

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
thanx a lot to CaKiwi and PHV. I tested it works wonders for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top