1837097 Members
2231 Online
110112 Solutions
New Discussion

Re: awk

 
SOLVED
Go to solution
Karen Hall
Occasional Contributor

awk

I have a file consisting of two columns separated by a space. I can assign a variable name to one column by doing:

for i in `awk '{print $1} ' basefile`

But how do I assign a variable name to the column 2 value?

basefile
--------
john nancy
gene esther
mike ruth
5 REPLIES 5
John Poff
Honored Contributor

Re: awk

Hi,

Use the $2 parameter for the value in the second column. To loop on the values of the second column try this:

for i in `awk '{print $2}' basefile`


I'm just curious. What are you trying to do with it anyway?

JP


P.S. Get ready for the Perl wizards to descend on you! ;)





A. Clay Stephenson
Acclaimed Contributor
Solution

Re: awk

It's really pretty easy:

Because the entries are whitespace separated you really don't even need awk.

cat basefile | while read A B
do
echo "1st var = ${A} 2nd var = ${B}"
done
If it ain't broke, I can fix that.
MANOJ SRIVASTAVA
Honored Contributor

Re: awk

cat basefile | awk '{print $1}' > abc
cat basefile | awk '{print $2}' >> abc

for i in 'awk {print $1 }' `abc`




Manoj Srivastava
Karen Hall
Occasional Contributor

Re: awk

A.Clay, you're right! That works great. Sometimes you can't see the forest for the trees. Thanks!
Fred Martin_1
Valued Contributor

Re: awk

As an aside, I strongly recommend you get a copy of the paperback "The AWK Programming Language" by Aho, Weinberger, Kernighan (AWK's developers).

It's the most concise yet complete manual for anything I've ever seen, and covers AWK very simply.
fmartin@applicatorssales.com