What's wrong with this??? 
Author Message
 What's wrong with this???

What is wrong with the following expression?
I want to get only the 3rd column,but I get all the file.
(e.g. - awk '{ print 3 }' file - works ok,but I'd like to use a variable)

i=3
echo $i
#3
awk '{ print $i }' file



Wed, 23 Jan 2002 03:00:00 GMT  
 What's wrong with this???

Quote:

>What is wrong with the following expression?
>I want to get only the 3rd column,but I get all the file.
>(e.g. - awk '{ print 3 }' file - works ok,but I'd like to use a variable)

>i=3
>echo $i
>#3
>awk '{ print $i }' file

awk '{print $3}' infile

Or you could do:

awk '{col=3; print $col}' infile

or

awk 'i=3; print $i}' infile

or if you insist on using shell variables:

i=3
awk '{print $col}' col=$i infile

or if you want to play games with quoting:

i=3
awk '{print $'"$i"'}' infile

but that seems a bit much and

awk '{print $3}' infile

seems easier if the column doesn't change.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 23 Jan 2002 03:00:00 GMT  
 What's wrong with this???
U need to declare your variable within an action

{i=3
print $(i)

Quote:
}

Cool by the pool
Quote:

>What is wrong with the following expression?
>I want to get only the 3rd column,but I get all the file.
>(e.g. - awk '{ print 3 }' file - works ok,but I'd like to use a variable)

>i=3
>echo $i
>#3
>awk '{ print $i }' file



Thu, 24 Jan 2002 03:00:00 GMT  
 What's wrong with this???
If you want to pass the variable value into the awk script from an external
shell script, then you must use argv.


Quote:
> What is wrong with the following expression?
> I want to get only the 3rd column,but I get all the file.
> (e.g. - awk '{ print 3 }' file - works ok,but I'd like to use a variable)

> i=3
> echo $i
> #3
> awk '{ print $i }' file



Fri, 25 Jan 2002 03:00:00 GMT  
 What's wrong with this???

Quote:

>If you want to pass the variable value into the awk script
>from an external shell script, then you must use argv.

Not true, you can also use this method:

i=3
awk '{print $col}' col=$i infile

or the POSIX -v option for awk/gawk:

gawk -v col=$i '{print $col}' infile

re-read the man pages

man awk
man gawk

Chuck Demas
Needham, Mass.

Quote:



>> What is wrong with the following expression?
>> I want to get only the 3rd column,but I get all the file.
>> (e.g. - awk '{ print 3 }' file - works ok,but I'd like to use a variable)

>> i=3
>> echo $i
>> #3
>> awk '{ print $i }' file

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 25 Jan 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. what's wrong with C#

2. What's wrong with apl\11?

3. Cincom Smalltalk - what's wrong here?

4. What's wrong with this?

5. What's wrong with this picture?

6. What's wrong with this code?

7. What's wrong with this code?

8. What's Wrong with this Line?

9. What's wrong with this ???

10. What's wrong with this?

11. What's wrong?

12. What's wrong??

 

 
Powered by phpBB® Forum Software