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

awk

Status
Not open for further replies.

we2aresame

Technical User
Feb 10, 2003
128
CA
we can
cat filename | awk '{print $1}'
cat filename | awk '{print $2}'
...
how to

for i in 1 2 3
do
cat filename | awk '{print $i}'
done

The (print $i ) doesn't work, why? Thanks.
 
# depending what you're after.
# What ARE you after?
#
for i in 1 2 3
do
awk -v i=${i} '{print $i}' filename
done

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
... and how's that UNIX related? [wink]

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi

I tried the given solution , which is
for i in 1 2 3
do
awk -v i=${i} '{print $i}' filename
done
,But its not wokring for me.

Then i tried like this,
for i in 1 2 3
do
awk '{ print i }' i=$i filename
done
, In this way its working for me. WHY?

thanx
baig.
 
you probably have an older awk without the '-v' switching.

What OS are you one?
If on Solaris, try 'nawk' instead of 'awk' with the original code.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
For all flavor of awk:
Code:
for i in 1 2 3
do
  awk '{print $'&quot;$i&quot;'}' filename
done

Hope This Help
PH.
 
Hi vlad/PH

Sorry I took a long time to check this program.

Well, my OS informations are
SunOS 5.6 Generic_105181-28 sun4u sparc SUNW,Ultra-Enterprise

It has both awk and nawk.

As you said(vlad), i tried using nawk, its working fine only after i removed the &quot;$&quot; symbol i.e instead of &quot;print $i&quot; used &quot;print i&quot; .

I tried the &quot;print $'&quot;$i&quot;'}&quot; also, but it seems to be having the same problem.

I am not sure whethere this awk is updated one or not.

thanx
baig.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top