elementary question 
Author Message
 elementary question

Hi,
this is my first experience with awk and solution
probably very simple - noyt to me though. I am trying to pipe
an ascii file with multiple columns to print just 3.
It works ok as long as I don't use a variable ie.
say

| awk '{print $7, $6, $9}'

if I do

| awk '{print $7, $6, $i}'

where $i is 9 or so awk messes up.

Would very much appreciate help.

Thanks,
Mariusz



Sat, 23 Apr 2005 01:02:10 GMT  
 elementary question

Quote:
> Hi,
> this is my first experience with awk and solution
> probably very simple - noyt to me though. I am trying to pipe
> an ascii file with multiple columns to print just 3.
> It works ok as long as I don't use a variable ie.
> say

> | awk '{print $7, $6, $9}'

> if I do

> | awk '{print $7, $6, $i}'

> where $i is 9 or so awk messes up.

> Would very much appreciate help.

> Thanks,
> Mariusz

Hi, I think you have shell variables confused with AWK variables.

Quote:
> | awk '{print $7, $6, $i}'

> where $i is 9 or so awk messes up.

You didn't show where you set i.  Try:

     ... | awk 'BEGIN{i=9}{print $7, $6, $i}'

or

    ... | awk '{i=9; print $7, $6, $i}'

or

    ... | awk -v i=9 '{print $7, $6, $i}'

or, in a bash script

    i=9
    awk -v i=${i} '{print $7, $6, $i}'

    - Dan



Sat, 23 Apr 2005 01:24:18 GMT  
 elementary question
Submitted by "Dan Haygood" to comp.lang.awk:

Quote:


>> Hi,
>> this is my first experience with awk and solution
>> probably very simple - noyt to me though. I am trying to pipe
>> an ascii file with multiple columns to print just 3.
>> It works ok as long as I don't use a variable ie.
>> say

>> | awk '{print $7, $6, $9}'

>> if I do

>> | awk '{print $7, $6, $i}'

>> where $i is 9 or so awk messes up.

>> Would very much appreciate help.

>> Thanks,
>> Mariusz

> Hi, I think you have shell variables confused with AWK variables.

>> | awk '{print $7, $6, $i}'

>> where $i is 9 or so awk messes up.

> You didn't show where you set i.  Try:

>      ... | awk 'BEGIN{i=9}{print $7, $6, $i}'

Here, 'i' and '$i' are still two different things, AFAIK.

If you want to use the shell variable '$i':

awk '{ print $1, $2, '"$i"' }'

If you want to use the awk variable 'i':

awk 'BEGIN { i = 10 } { print $1, $2, i }'

--
Andreas K?h?ri               --==::{ Have a Unix: netbsd.org
                             --==::{ This post ends with :wq



Sat, 23 Apr 2005 04:08:22 GMT  
 elementary question

Quote:
> Submitted by "Dan Haygood" to comp.lang.awk:


> >> Hi,
> >> this is my first experience with awk and solution
> >> probably very simple - noyt to me though. I am trying to pipe
> >> an ascii file with multiple columns to print just 3.
> >> It works ok as long as I don't use a variable ie.
> >> say

> >> | awk '{print $7, $6, $9}'

> >> if I do

> >> | awk '{print $7, $6, $i}'

> >> where $i is 9 or so awk messes up.

> >> Would very much appreciate help.

> >> Thanks,
> >> Mariusz

> > Hi, I think you have shell variables confused with AWK variables.

> >> | awk '{print $7, $6, $i}'

> >> where $i is 9 or so awk messes up.

> > You didn't show where you set i.  Try:

> >      ... | awk 'BEGIN{i=9}{print $7, $6, $i}'

> Here, 'i' and '$i' are still two different things, AFAIK.

> If you want to use the shell variable '$i':

> awk '{ print $1, $2, '"$i"' }'

> If you want to use the awk variable 'i':

> awk 'BEGIN { i = 10 } { print $1, $2, i }'

> --
> Andreas K?h?ri               --==::{ Have a Unix: netbsd.org
>                              --==::{ This post ends with :wq

Hi Andreas -

Quote:
> >      ... | awk 'BEGIN{i=9}{print $7, $6, $i}'
> Here, 'i' and '$i' are still two different things, AFAIK.

You are correct.  i is an AWK variable; $i is the i-th field from the input
line to awk, equivalent to the awk expression $(i); in this case, i == 9, so
$i is $9, or the ninth field in the input to awk (not to be confused with
$9, the ninth parameter from the command line used to invoke a script).  As
you observed, this is not the shell variable i.

I thought I had made myself clear, that this was to illustrate the
difference between using a 'shell variable' and 'an AWK variable in a
field-extraction expression'.

Quote:
> If you want to use the shell variable '$i':
> awk '{ print $1, $2, '"$i"' }'

Yes, that's one way.  Your double-quotes are unnecessary in most shells.  If
the OP wanted, they could also do

    i=9
    ... | awk 'BEGIN{i='$i'}{print $7, $6, $i}'

Quote:
> If you want to use the awk variable 'i':
> awk 'BEGIN { i = 10 } { print $1, $2, i }'

But the OP wanted to print (per your example) the tenth field of the input
line to awk, not the number 10.

    - Dan



Sat, 23 Apr 2005 11:49:22 GMT  
 elementary question
Submitted by "Dan Haygood" to comp.lang.awk:

Quote:

[cut]
> Hi Andreas -

Good evening Dan,

Quote:

>> >      ... | awk 'BEGIN{i=9}{print $7, $6, $i}'
>> Here, 'i' and '$i' are still two different things, AFAIK.

> You are correct. i is an AWK variable; $i is the i-th field
> from the input line to awk, equivalent to the awk expression
> $(i); in this case, i == 9, so $i is $9, or the ninth field
> in the input to awk (not to be confused with $9, the ninth
> parameter from the command line used to invoke a script).  As
> you observed, this is not the shell variable i.

> I thought I had made myself clear, that this was to illustrate
> the difference between using a 'shell variable' and 'an AWK
> variable in a field-extraction expression'.

In that case I apologise for not paying close enough attention
to your post.

Quote:

>> If you want to use the shell variable '$i':
>> awk '{ print $1, $2, '"$i"' }'

> Yes, that's one way.  Your double-quotes are unnecessary in
> most shells.  If the OP wanted, they could also do

>     i=9
>     ... | awk 'BEGIN{i='$i'}{print $7, $6, $i}'

You're perfectly clear now.  I have seen the light.  Sorry for
jumping out at you.

--
Andreas K?h?ri               --==::{ Have a Unix: netbsd.org
                             --==::{ This post ends with :wq



Sat, 23 Apr 2005 12:46:52 GMT  
 elementary question
Try explicitly 'i=9;' :

awk '{i=9;print $7, $6, $i}'

but be sure that for each line NF>=9

E.g.
awk '{for(i=1;i<=NF;i++){printf(" %s",$i);};printf("\n");}'
should always work.


Quote:
> Hi,
> this is my first experience with awk and solution
> probably very simple - noyt to me though. I am trying to pipe
> an ascii file with multiple columns to print just 3.
> It works ok as long as I don't use a variable ie.
> say

> | awk '{print $7, $6, $9}'

> if I do

> | awk '{print $7, $6, $i}'

> where $i is 9 or so awk messes up.

> Would very much appreciate help.

> Thanks,
> Mariusz



Sun, 24 Apr 2005 00:32:39 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. elementary question

2. elementary question, IBM (LCSI) Logo v 1.0 (!)

3. (elementary question) Test on type ?

4. Elementary question

5. elementary question about gdb with g77

6. Elementary question

7. Newbie elementary question

8. Newbie elementary question (please help!!!)

9. File handles and streams (elementary questions)

10. Yet another elementary question on f90

11. An elementary question on f90

12. Elementary question: returning from within a function

 

 
Powered by phpBB® Forum Software