Computing.Net > Forums > Unix > Using AWK

Using AWK

Reply to Message Icon

Original Message
Name: thoppz
Date: November 19, 2004 at 15:26:37 Pacific
Subject: Using AWK
OS: hp-ux 11.0
CPU/Ram: 1G
Comment:

I've a loop to read a file line by line.
Now each line has 4 columns seperated by space.
How do I get 4 diff column values using awk to 4 seperate variables?
eg of a line: adas 343434 adas@adas.org Informationsystems

n=`wc -l < get_dwsdid.txt`
i=1

while [ "$i" -le "$n" ]
do
line=`cat get_dwsdid.txt | head -$i | tail -1`

echo $line
i=`expr $i + 1`

done


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: November 20, 2004 at 05:55:41 Pacific
Subject: Using AWK
Reply: (edit)

The following code will pass the file only once, reading the first 4 words of each line into the variables:

while read a b c d excess
do
echo "a=$a b=$b c=$c d=$d"
done < get_dwsdid.txt


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: November 20, 2004 at 10:21:17 Pacific
Subject: Using AWK
Reply: (edit)

Hi:

First, I'd take Jim's advice; I'ts the most efficient. Another way is to use the set command to parse the lines read into shell variables:


while read line
do
set `echo $line`
echo $1
echo $2
echo $3
echo $4

done < get_dwsdid.txt

If you really insist on using awk, you can use it with the eval command:


while read line
do
eval `echo $line|awk ' { printf("a=%s; b=%s; c=%s; d=%s\n", $1, $2, $3, $4) } '`
echo $a
echo $b
echo $c
echo $d

done < get_dwsdid.txt

Regards,


Nails


Report Offensive Follow Up For Removal

Response Number 3
Name: thoppz
Date: November 22, 2004 at 12:35:09 Pacific
Subject: Using AWK
Reply: (edit)

Jim & Nails,

I tried both your suggestions and it just worked great!!

Thank you guys very much.
Sam.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Using AWK

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software