Passing variables into awk. 
Author Message
 Passing variables into awk.

I'm having trouble with the syntax for passing variable to
awk without creating a separate awk file.

    **File tmp.122138 contains this:

    ------------------------------------
    |ID      |REP                  |
    ------------------------------------
    |rb0751  |AAtestadam    |
    |rb0752  |BBtestfred      |
    |rb9009  |CCtestfred      |
    ------------------------------------

    **I want the value of the second field, so I type this:

    awk -F"|" '/\|rb0751/  {print $3}' tmp.122138
    AAtestadam

    **This works but my id field (rb0751) will change, so I need to pass a
      variable in for the  current value.   I've looked at many books, but
      I can't find one which shows how this is done.   I'm looking for
      something like:
        VARIABLE=rb9009
        awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

Thanks for any help,
                   Adam



Sat, 26 Feb 2000 03:00:00 GMT  
 Passing variables into awk.

    Adam> I'm having trouble with the syntax for passing variable to awk
    Adam> without creating a separate awk file.

    Adam>     **File tmp.122138 contains this:

    Adam>     ------------------------------------
    Adam>     |ID      |REP                  |
    Adam>     ------------------------------------
    Adam>     |rb0751  |AAtestadam    |
    Adam>     |rb0752  |BBtestfred      |
    Adam>     |rb9009  |CCtestfred      |
    Adam>     ------------------------------------

    Adam>     **I want the value of the second field, so I type this:

    Adam>     awk -F"|" '/\|rb0751/  {print $3}' tmp.122138
    Adam>     AAtestadam

    Adam> **This works but my id field (rb0751) will change, so I need
    Adam> to pass a variable in for the current value.  I've looked at
    Adam> many books, but I can't find one which shows how this is done.
    Adam> I'm looking for something like:

    Adam>       VARIABLE=rb9009
    Adam>       awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

    Adam> Thanks for any help, Adam

If it's still not in the FAQ, could it be added? This was answered too
many times to be left out.

The following should work:

    awk -F'|' -v id=$VARIABLE '$2 ~ "^" id {print $3}' tmp.122138

If your AWK does not accept `-v', it's too old and asking for
upgrade. Anyway, the following should work even in older AWKs:

    awk -F'|' '$2 ~ "^" id {print $3}' id=$VARIABLE tmp.122138

--
--------------------------------------------------------------------------------
 .......                                       Yuli Barcohen
    .    ...... .      .....    ..   .....
    .    .      .      .    .  .  .  .    .    Software tools development
    .    .....  .      .    . .    . .    .
    .    .      .      .....  ...... .    .    Phone  972-8-927-3326
    .    .      .      .   .  .    . .    .    Fax    972-8-927-3487

--------------------------------------------------------------------------------



Sat, 26 Feb 2000 03:00:00 GMT  
 Passing variables into awk.


[...]

%       I'm looking for something like:
%         VARIABLE=rb9009
%         awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

You can try
 awk -v var=$VARIABLE -F"|" '$2 == var { print $3 }' tmp.122138
 #        I guess this will never change, eh?  ^^^^^^^^^^

or, if you really love your shell, you can try
 awk -F"|" '/\|'$VARIABLE'/ { print $3 }' tmp.122138

Hope one of those helps.
--

Patrick TJ McPhee
East York  Canada



Sat, 26 Feb 2000 03:00:00 GMT  
 Passing variables into awk.

Quote:

> If it's still not in the FAQ, could it be added? This was
> answered too many times to be left out.

it has been in the FAQ for quite a while now.

FAQ> 14. How can I access shell or environment variables in an awk script?
FAQ>   14.1 Environment variables in general
FAQ>   14.2 Unix Shell Quoting
FAQ>   14.3 ENVIRON and "env"|
FAQ>   14.4 exporting environment variables back to the parent process
--



Sun, 27 Feb 2000 03:00:00 GMT  
 Passing variables into awk.


Quote:

> I'm having trouble with the syntax for passing variable to
> awk without creating a separate awk file.
>     **I want the value of the second field, so I type this:

>     awk -F"|" '/\|rb0751/  {print $3}' tmp.122138
>     AAtestadam

>     **This works but my id field (rb0751) will change, so I need to
pass a
>       variable in for the  current value.   I've looked at many books,
but
>       I can't find one which shows how this is done.   I'm looking for
>       something like:
>         VARIABLE=rb9009
>         awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

> Thanks for any help,
>                    Adam

In a book "Programming Language AWK" , the way of passing shell variables
into awk script
is explaind. I recommend to read the book.

I usually use technique such as,

-----------------------
#!/bin/sh

string=$1

gawk '
BEGIN{string = "'"$string"'"}
{
   if($1 ~ string) print $0

Quote:
}' $2

------------------------

Regards,

Manabu Kunimoto

--
Manabu Kunimoto



Mon, 28 Feb 2000 03:00:00 GMT  
 Passing variables into awk.

:         VARIABLE=rb9009
:         awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138
:      

VARIABLE=rb9009
awk -F"|" '/\|'"$VARIABLE"'/  {print $3}' tmp.122138

This should work. (for var tobe treated as a shell variable use '"$var"' ).

bye
Praveen



Mon, 28 Feb 2000 03:00:00 GMT  
 Passing variables into awk.

Quote:

>>I'm having trouble with the syntax for passing variable to
>>awk without creating a separate awk file.

>>    **File tmp.122138 contains this:

>>    ------------------------------------
>>    |ID      |REP                  |
>>    ------------------------------------
>>    |rb0751  |AAtestadam    |
>>    |rb0752  |BBtestfred      |
>>    |rb9009  |CCtestfred      |
>>    ------------------------------------

>>    **I want the value of the second field, so I type this:

>>    awk -F"|" '/\|rb0751/  {print $3}' tmp.122138
>>    AAtestadam

>>    **This works but my id field (rb0751) will change, so I need to pass a
>>      variable in for the  current value.   I've looked at many books, but
>>      I can't find one which shows how this is done.   I'm looking for
>>      something like:
>>        VARIABLE=rb9009
>>        awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

>>Thanks for any help,
>>                   Adam

You have 2 choices:

1)      Pass it in via the -v argument:

        VARIABLE=rb9009
        awk -F"|" -v KEY="$VARIABLE"        \
                '$2 == KEY  {print $3}' tmp.122138

2)      Try using the ENVIRON associative array (the shell
        variable must be exported):

        export VARIABLE=rb9009
        awk -F"|" '$2 == ENVIRON[VARIABLE]  {print $3}' tmp.122138

        If the file is really long, you might want to use a temp
        variable to avoid the associative array lookup each time:

        export VARIABLE=rb9009
        awk -F"|" 'BEGIN { KEY == ENVIRON[VARIABLE] }
                $2 == KEY  {print $3}' tmp.122138

        ** Try this little test program to show your environment **
        awk 'BEGIN  {
                for (v in ENVIRON)
                        printf("%s=%s\n", v, ENVIRON[v])
        }'

Cary Coulter



Tue, 29 Feb 2000 03:00:00 GMT  
 Passing variables into awk.

Quote:


> >>I'm having trouble with the syntax for passing variable to
> >>awk without creating a separate awk file.

> >>    **File tmp.122138 contains this:

> >>    ------------------------------------
> >>    |ID      |REP                  |
> >>    ------------------------------------
> >>    |rb0751  |AAtestadam    |
> >>    |rb0752  |BBtestfred      |
> >>    |rb9009  |CCtestfred      |
> >>    ------------------------------------

> >>    **I want the value of the second field, so I type this:

> >>    awk -F"|" '/\|rb0751/  {print $3}' tmp.122138
> >>    AAtestadam

> >>    **This works but my id field (rb0751) will change, so I need to pass a
> >>      variable in for the  current value.   I've looked at many books, but
> >>      I can't find one which shows how this is done.   I'm looking for
> >>      something like:
> >>        VARIABLE=rb9009
> >>        awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

> >>Thanks for any help,
> >>                   Adam

    You may try:

    VAR1=rb9009
    awk -v VAR=$VAR1 ' index( $0, VAR) '

    or even pass regular expressions using match()

    VAR1="rb90.*9"
    awk -v VAR="$VAR1"  ' match( $0, VAR ) '

Radoslav Nedjalkov




Sat, 11 Mar 2000 03:00:00 GMT  
 Passing variables into awk.

Almost Radoslav!
You forgot to mention that one must use nawk........

:)  AW

Quote:



> > >>I'm having trouble with the syntax for passing variable to
> > >>awk without creating a separate awk file.

> > >>    **File tmp.122138 contains this:

> > >>    ------------------------------------
> > >>    |ID      |REP                  |
> > >>    ------------------------------------
> > >>    |rb0751  |AAtestadam    |
> > >>    |rb0752  |BBtestfred      |
> > >>    |rb9009  |CCtestfred      |
> > >>    ------------------------------------

> > >>    **I want the value of the second field, so I type this:

> > >>    awk -F"|" '/\|rb0751/  {print $3}' tmp.122138
> > >>    AAtestadam

> > >>    **This works but my id field (rb0751) will change, so I need to pass a
> > >>      variable in for the  current value.   I've looked at many books, but
> > >>      I can't find one which shows how this is done.   I'm looking for
> > >>      something like:
> > >>        VARIABLE=rb9009
> > >>        awk -F"|" '/\|$VARIABLE/  {print $3}' tmp.122138

> > >>Thanks for any help,
> > >>                   Adam

>     You may try:

>     VAR1=rb9009
>     awk -v VAR=$VAR1 ' index( $0, VAR) '

>     or even pass regular expressions using match()

>     VAR1="rb90.*9"
>     awk -v VAR="$VAR1"  ' match( $0, VAR ) '

> Radoslav Nedjalkov





Mon, 13 Mar 2000 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. : Passing Variable to awk

2. passing variables to awk scripts

3. Passing command line variables to AWK in shell script

4. How to pass shell variable to awk?

5. Passing the current directory path to a variable - awk on a PC

6. search on variable passed to awk /nawk

7. Passing an environment variable to a system call within awk

8. pass a variable from awk to shell

9. Pass an AWK variable to a pipeline program as a parameter

10. passing shell variable into awk program

11. how do i pass shell variables into awk?

12. passing shell variable containing many fields to awk

 

 
Powered by phpBB® Forum Software