awk: -v Var=VAR Obviously not a syntax issue

awk: -v Var=VAR Obviously not a syntax issue

Post by /sbin/shutdown -h no » Sun, 29 Apr 2001 16:40:01



I'm doing a simple match and print lines.  i suspect the problem is in the
way
i match, but not sure.

This is easy:

when the match string is hardcoded, no problem


want;
print line; print ; getline ; line = line + 1 } }' ./sed_clean.tmp

CABLETRON
1
        CABLETRON -CS  
CABLETRON
2
        14.55

But since i am iterating a list, i need awk to take a variable.
i thought it was a syntax issue, but apparently awk knows about
the variable:


./sed_clean.tmp
CABLETRON

This proves
1. it is matching that string
2. it knows and prints the value of the commandline variable.

Why does this then fail:


{
print want; print line; print ; getline ; line = line + 1 } }'
./sed_clean.tmp

<No output>

That's outrageous!

------------------------------------------------------------
ls -l *.mktg
-rw-rw-r--   1 manager   mktg   987526521872  Dec 10 01:49 sales.mktg
sed -e '/^LIES/d' sales.mktg > truth.mktg
ls -l *.mktg
-rw-rw-r--  1 manager    mktg   987526521872  Dec 10 01:49 sales.mktg
-rw-rw-r--  1 root       root              0  Dec 10 01:49 truth.mktg

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Michael Heimin » Sun, 29 Apr 2001 20:18:33




> ./sed_clean.tmp
> CABLETRON

[SNIP]

Hello,

don't know what you're trying, however the syntax is:

awk -v Var1=$VAR1 -v Var2=$VAR2 '{...

Where "Varn" is the variable awk gets and "$VARn" is the shell variable
you
want to hand over to awk.

Good luck

Michael Heiming

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Matt Ve » Sun, 29 Apr 2001 21:30:08



Quote:> I'm doing a simple match and print lines.  i suspect the problem
> is in the way i match, but not sure.

Yes, it is.

Quote:> gawk -v want=CABLETRON '/want/ { line == 0; while (line <= 2) ...

Gawk thinks that you are wanting to match the literal string "want",
and is not doing variable interpolation in regexes (Perl style).

You can make use of dynamic regexps like this:

regexp = "123.*[a-z]+"
$0 ~ regexp

So your line above would become:

gawk -v want=CABLETRON '$0 ~ want { line == 0; while (line <= 2) ...

Matt

--
#!/usr/bin/perl
$A='A';while(print+($A.=(grep{($A=~/(...).{78}$/)[0]eq$_}"  A A A  "
=~m{(...)}g)?"A":" ")=~/([ A])$/){if(!(++$l%80)){print"\n";sleep 1}}

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Kenny McCorma » Sun, 29 Apr 2001 22:25:20





>> I'm doing a simple match and print lines.  i suspect the problem
>> is in the way i match, but not sure.

>Yes, it is.

>> gawk -v want=CABLETRON '/want/ { line == 0; while (line <= 2) ...

>Gawk thinks that you are wanting to match the literal string "want",
>and is not doing variable interpolation in regexes (Perl style).

>You can make use of dynamic regexps like this:

>regexp = "123.*[a-z]+"
>$0 ~ regexp

>So your line above would become:

>gawk -v want=CABLETRON '$0 ~ want { line == 0; while (line <= 2) ...

I know this is the standard given advice, but I don't think it is the best
advice.  Explaining how to use "-v" correctly adds two layers of complexity
that the naive user may not be comfortable with - namely:
        1) How to use -v correctly (the exact semantics of how it works
           varies somewhat from version to version of AWK).  Also, one has
           to deal with quoting issues if the string has embedded blanks or
           shell metas.
        2) How to use the variable correctly inside of AWK - Note that this
           question always comes up because the user doesn't understand AWK
           as AWK (doesn't understand that AWK has variables in the
           traditional programming language sense of variables) and sees
           it as merely another version of sed or grep.

For both of the above reasons, it has got to be better advice to just say:

#!/bin/sh
want=CABLETRON
gawk "/$want/"' { line == 0; while (line <= 2) ...'

Anyway, this is how I usually do it.

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Matt Ve » Mon, 30 Apr 2001 01:49:54


Quote:>    1) How to use -v correctly (the exact semantics of how it works
>       varies somewhat from version to version of AWK).  Also, one has
>       to deal with quoting issues if the string has embedded blanks or
>       shell metas.

Agreed, and the quoting issue has always been a problem for me.

Quote:>    2) How to use the variable correctly inside of AWK - Note that this
>       question always comes up because the user doesn't understand AWK
>       as AWK (doesn't understand that AWK has variables in the
>       traditional programming language sense of variables) and sees
>       it as merely another version of sed or grep.

Fair point, but where are you going to get with Awk unless you
treat it like a programming language?

Quote:>#!/bin/sh
>want=CABLETRON
>gawk "/$want/"' { line == 0; while (line <= 2) ...'

Cool, I'll have to remember that in future.

Matt

--
#!/usr/bin/perl
$A='A';while(print+($A.=(grep{($A=~/(...).{78}$/)[0]eq$_}"  A A A  "
=~m{(...)}g)?"A":" ")=~/([ A])$/){if(!(++$l%80)){print"\n";sleep 1}}

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Michael Mau » Mon, 30 Apr 2001 04:11:41



>>       2) How to use the variable correctly inside of AWK - Note that this
>>          question always comes up because the user doesn't understand AWK
>>          as AWK (doesn't understand that AWK has variables in the
>>          traditional programming language sense of variables) and sees
>>          it as merely another version of sed or grep.

> Fair point, but where are you going to get with Awk unless you
> treat it like a programming language?

You get to write scripts with something like

    cat foo | grep bar | grep -v baz | awk '{print $3}'

- probably inside a loop that calls this and similar constructs a
thousand times. And soon you decide that "awk is slow, because it's only
an interpreted language". Try a search on groups.google.com for "grep
awk" and you will find hundreds of similar "gems".

SCNR

Regards...
                Michael

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Patrick TJ McPh » Mon, 30 Apr 2001 07:28:58



% I know this is the standard given advice, but I don't think it is the best
% advice.  Explaining how to use "-v" correctly adds two layers of complexity
% that the naive user may not be comfortable with - namely:

I'm going to disagree with you for a handful of reasons:

 1. in this case, the original poster knew how to use -v, and simply
    didn't know about the ~ operator
 2. you've got quoting issues whether you pass a shell variable as part
    of a -v argument or as part of the program. The quoting issues when
    interpolating a shell variable into the text of a script are always
    more complicated and lead to substantial amounts of traffic in
    comp.lang.awk (to which I've directed follow-ups, incidentally)
 3. you have to use -v if your awk script is included from a file using -f
 4. clever quoting can depend on the OS and shell in use for success. There
    is substantially greater variety on that front than there is between
    different versions of awk wrt support for -v

--

Patrick TJ McPhee
East York  Canada

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by Benjamin.Altma » Tue, 01 May 2001 23:56:01


The use of -v var=$VAR is only useful if you need the variable in the BEGIN
section of the awk script.  If its not in the BEGIN part then you can place it
as a part of the file list as follows:
    awk '{ .... }' var=$VAR file1 file2 etc.

Ben





> >> I'm doing a simple match and print lines.  i suspect the problem
> >> is in the way i match, but not sure.

> >Yes, it is.

> >> gawk -v want=CABLETRON '/want/ { line == 0; while (line <= 2) ...

> >Gawk thinks that you are wanting to match the literal string "want",
> >and is not doing variable interpolation in regexes (Perl style).

> >You can make use of dynamic regexps like this:

> >regexp = "123.*[a-z]+"
> >$0 ~ regexp

> >So your line above would become:

> >gawk -v want=CABLETRON '$0 ~ want { line == 0; while (line <= 2) ...

> I know this is the standard given advice, but I don't think it is the best
> advice.  Explaining how to use "-v" correctly adds two layers of complexity
> that the naive user may not be comfortable with - namely:
>         1) How to use -v correctly (the exact semantics of how it works
>            varies somewhat from version to version of AWK).  Also, one has
>            to deal with quoting issues if the string has embedded blanks or
>            shell metas.
>         2) How to use the variable correctly inside of AWK - Note that this
>            question always comes up because the user doesn't understand AWK
>            as AWK (doesn't understand that AWK has variables in the
>            traditional programming language sense of variables) and sees
>            it as merely another version of sed or grep.

> For both of the above reasons, it has got to be better advice to just say:

> #!/bin/sh
> want=CABLETRON
> gawk "/$want/"' { line == 0; while (line <= 2) ...'

> Anyway, this is how I usually do it.

 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by /sbin/shutdown -h no » Tue, 08 May 2001 18:11:28





>> ./sed_clean.tmp
>> CABLETRON

>[SNIP]

>Hello,

>don't know what you're trying, however the syntax is:

>awk -v Var1=$VAR1 -v Var2=$VAR2 '{...

yeah, thanks.

Problem is that instead of passing a SHELL VARIABLE i was passing a VALUE.
Oh
wwll

Quote:

>Where "Varn" is the variable awk gets and "$VARn" is the shell variable
>you
>want to hand over to awk.

>Good luck

>Michael Heiming

------------------------------------------------------------
ls -l *.mktg
-rw-rw-r--   manager mktg   987526521872  sales.mktg
sed -e '/^LIES/d' sales.mktg > truth.mktg
ls -l *.mktg
-rw-rw-r--  manager  mktg   987526521872  sales.mktg
-rw-rw-r--  root     root              0  truth.mktg
 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by /sbin/shutdown -h no » Tue, 08 May 2001 18:41:28




>> I'm doing a simple match and print lines.  i suspect the problem
>> is in the way i match, but not sure.

>Yes, it is.

>> gawk -v want=CABLETRON '/want/ { line == 0; while (line <= 2) ...

>Gawk thinks that you are wanting to match the literal string "want",
>and is not doing variable interpolation in regexes (Perl style).

>You can make use of dynamic regexps like this:

>regexp = "123.*[a-z]+"
>$0 ~ regexp

>So your line above would become:

>gawk -v want=CABLETRON '$0 ~ want { line == 0; while (line <= 2) ...

>Matt

You Perl people think you know everything. :)

Thanks

Quote:

>--
>#!/usr/bin/perl
>$A='A';while(print+($A.=(grep{($A=~/(...).{78}$/)[0]eq$_}"  A A A  "
>=~m{(...)}g)?"A":" ")=~/([ A])$/){if(!(++$l%80)){print"\n";sleep 1}}

------------------------------------------------------------
ls -l *.mktg
-rw-rw-r--   manager mktg   987526521872  sales.mktg
sed -e '/^LIES/d' sales.mktg > truth.mktg
ls -l *.mktg
-rw-rw-r--  manager  mktg   987526521872  sales.mktg
-rw-rw-r--  root     root              0  truth.mktg
 
 
 

awk: -v Var=VAR Obviously not a syntax issue

Post by /sbin/shutdown -h no » Tue, 08 May 2001 18:46:01


=====

Quote:>The use of -v var=$VAR is only useful if you need the variable in the BEGIN
>section of the awk script.  If its not in the BEGIN part then you can place

it

Quote:>as a part of the file list as follows:
>    awk '{ .... }' var=$VAR file1 file2 etc.

>Ben

Yeah i later RTFM again and saw that i could do that too

Thanks





>> >> I'm doing a simple match and print lines.  i suspect the problem
>> >> is in the way i match, but not sure.

>> >Yes, it is.

>> >> gawk -v want=CABLETRON '/want/ { line == 0; while (line <= 2) ...

>> >Gawk thinks that you are wanting to match the literal string "want",
>> >and is not doing variable interpolation in regexes (Perl style).

>> >You can make use of dynamic regexps like this:

>> >regexp = "123.*[a-z]+"
>> >$0 ~ regexp

>> >So your line above would become:

>> >gawk -v want=CABLETRON '$0 ~ want { line == 0; while (line <= 2) ...

>> I know this is the standard given advice, but I don't think it is the best
>> advice.  Explaining how to use "-v" correctly adds two layers of complexity
>> that the naive user may not be comfortable with - namely:
>>         1) How to use -v correctly (the exact semantics of how it works
>>            varies somewhat from version to version of AWK).  Also, one has
>>            to deal with quoting issues if the string has embedded blanks or
>>            shell metas.
>>         2) How to use the variable correctly inside of AWK - Note that this
>>            question always comes up because the user doesn't understand AWK
>>            as AWK (doesn't understand that AWK has variables in the
>>            traditional programming language sense of variables) and sees
>>            it as merely another version of sed or grep.

>> For both of the above reasons, it has got to be better advice to just say:

>> #!/bin/sh
>> want=CABLETRON
>> gawk "/$want/"' { line == 0; while (line <= 2) ...'

>> Anyway, this is how I usually do it.

------------------------------------------------------------
ls -l *.mktg
-rw-rw-r--   manager mktg   987526521872  sales.mktg
sed -e '/^LIES/d' sales.mktg > truth.mktg
ls -l *.mktg
-rw-rw-r--  manager  mktg   987526521872  sales.mktg
-rw-rw-r--  root     root              0  truth.mktg
 
 
 

1. sh var -> awk var -> sh var

I want to do something like the following:
---------------------------------------------------------------------
echo "-----------end----------------"
line_num=`expr $line_num + 1`
awk '{  
       for( ; n%=xx ; n++ )  <---xx means a number.
        printf("\n")
     }' n=$page_line          <---this need input, else it stop....
line_num=$n    <-----I want $line_num = n in awk.
---------------------------------------------------------------------
I need certain length of the outfiles, so if "----end----" end at line number
which is not enough, space lines are added to enough length number.
Thanks.

--
  =====================================================================
    Simon Tneoh Chee Boon  ~{UEV>ND~}  i?

    Hsin Chu City       NATIONAL TSING HUA UNI.
    P.O.Box 2-037       Material Science Engineering Dept. Batch '96

    R.O.China.          http://140.114.63.14:6083/simon.html
    886-35-715131-7300
  =====================================================================

2. trapping pts

3. Question: passing $var and "$var" into AWK from script

4. loop trying to go beyond end of device

5. Need help not passing make vars as env vars in gmake.

6. Cannon Copier Cartridges

7. shell var and awk (I'm trying export and ENVIRON but not working)

8. [kernel patch]

9. How large can /var/log/messages and /var/log/syslog get ?

10. trimming /var/adm/messages & /var/adm/syslog

11. How to close /var/log/syslog and /var/log/messages..

12. cd $var where var is a windows path with spaces in it

13. VAR=$(cmd) versus VAR=`cmd`