Computing.Net > Forums > Unix > Using AWK

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.

Using AWK

Reply to Message Icon

Name: thoppz
Date: November 19, 2004 at 15:26:37 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: November 20, 2004 at 05:55:41 Pacific
Reply:

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


0

Response Number 2
Name: nails
Date: November 20, 2004 at 10:21:17 Pacific
Reply:

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


0

Response Number 3
Name: thoppz
Date: November 22, 2004 at 12:35:09 Pacific
Reply:

Jim & Nails,

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

Thank you guys very much.
Sam.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: Using AWK

Using AWK www.computing.net/answers/unix/using-awk/6583.html

profiling using awk www.computing.net/answers/unix/profiling-using-awk/7035.html

arithmetic using awk www.computing.net/answers/unix/arithmetic-using-awk/7325.html