Re: Awk and assigning values to multiple variables (509 Views)
Reply
Super Advisor
Posts: 272
Registered: ‎05-14-2004
Message 1 of 10 (509 Views)
Accepted Solution

Awk and assigning values to multiple variables

I have a simple need that I know I can resolve with a loop but I want to know if there is a way to do it without looping.

My file has one line from TOP
0 ? 37 root 152 20 0K 1888K run 247:32 0.58 0.58 vxfsd

All I want to do is assign each piece of this line to a seperate variable. I could do
awk'{print $1" "$2 " "$3}' toptest.txt | while read var1 var2 var3; do
do whatever
done

but I would like to stay away from the loop since there is (and will only be) one line of data
Exalted Contributor
Posts: 33,806
Registered: ‎08-15-2002
Message 2 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

var1=$(awk'{print $1}' toptest.txt)
var2=$(awk'{print $2}' toptest.txt)
var3=$(awk'{print $3}' toptest.txt)

This should populate the variables without using a loop.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Super Advisor
Posts: 272
Registered: ‎05-14-2004
Message 3 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

That is another way, but I have 13 to do. I'm looking for the most efficient way. What do you think about doing 13 variables that way?
Trusted Contributor
Posts: 60
Registered: ‎10-05-2004
Message 4 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

being your file has just one line,
the simplest way with just using the shell, would be:
cat file | read var1 var2 var3 rest
print "$var1 $var2 $var3" #just to test

and if you don't mind reading from right to left:

read var1 var2 var3 rest < yourfile
Trusted Contributor
Posts: 278
Registered: ‎07-15-1999
Message 5 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

I think that you can do:

cat file | while read v1 v2 v3 ... v13
do
commands
end
Honored Contributor
Posts: 4,040
Registered: ‎06-18-2003
Message 6 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

Hi,
what about
read var1 var2 var3 < toptest.txt
?

greetings,

Michael
Trusted Contributor
Posts: 60
Registered: ‎10-05-2004
Message 7 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

and you could put them all into an array:
set -A array $(print ${array[0]}
Trusted Contributor
Posts: 60
Registered: ‎10-05-2004
Message 8 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

and if you don't need your positional variables anymore:

set -- $(yourfile)

print $1 $2

Trusted Contributor
Posts: 60
Registered: ‎10-05-2004
Message 9 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

oops

should have been

set -- $(
print $1 $2

forgot the < in $(
Super Advisor
Posts: 272
Registered: ‎05-14-2004
Message 10 of 10 (509 Views)

Re: Awk and assigning values to multiple variables

I'm going for the
read var1 var2 var3 < toptest.txt
method.

Thanks for all the input!!

Scott
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.