Awk Command-Line Variables 
Author Message
 Awk Command-Line Variables

I am trying to understand how awk uses the variable assignment on the
command-line.

Can I do this ?

cat text.file | awk '$1~/var/{print$3}' var=text

because I can not get this work for me.  However, this variable assignment
does work:

cat text.file | awk '{printf(" The value of var is: %s\n",var)}' var=text

It appears to work in the action portion of the awk program but not the
pattern-matching portion. I'm using a SUN machine running Solaris 2.8 or Sun
OS 5.8.

Perhaps you can only get it to pass the variable assignment in the action
portion of the program or when writing large programs and invoking awk like
this:

awk -f  '{program}' var=text

All responses appreciated,
Thanks, Steve.



Tue, 30 Nov 2004 06:19:49 GMT  
 Awk Command-Line Variables
On Thu, 13 Jun 2002 22:19:49 GMT,

Quote:
>I am trying to understand how awk uses the variable assignment on the
>command-line.

>Can I do this ?

>cat text.file | awk '$1~/var/{print$3}' var=text

awk '$1~var {print $3}' var=text <text.file
Quote:
>because I can not get this work for me.  However, this variable assignment
>does work:

>cat text.file | awk '{printf(" The value of var is: %s\n",var)}' var=text

>It appears to work in the action portion of the awk program but not the
>pattern-matching portion. I'm using a SUN machine running Solaris 2.8 or Sun
>OS 5.8.



Tue, 30 Nov 2004 06:44:42 GMT  
 Awk Command-Line Variables

Quote:
> I am trying to understand how awk uses the variable assignment on the
> command-line.

> Can I do this ?

> cat text.file | awk '$1~/var/{print$3}' var=text

                       ^^^^^^^^
As Bill has said this should be $1~var - the reasons are these:

1) /var/ is a static RE that consists of the literals 'v,'a','r' in that
order.
2) var is a dynamic RE where var is replaced by the value of var when
the RE is compiled, so that var's value is used as the RE.

Quote:

> because I can not get this work for me.  However, this variable
assignment
> does work:

> cat text.file | awk '{printf(" The value of var is: %s\n",var)}'
var=text

> It appears to work in the action portion of the awk program but not
the
> pattern-matching portion. I'm using a SUN machine running Solaris 2.8
or Sun
> OS 5.8.

I trust that you're using /usr/xpg4/bin/awk as that is allegedly POSIX
conforming.  If you're using /usr/bin/awk bear in mind that is pre-1987
vintage and doesn't support much of what appears in TAPL.

Quote:
> Perhaps you can only get it to pass the variable assignment in the
action
> portion of the program or when writing large programs and invoking awk
like
> this:

> awk -f  '{program}' var=text

> All responses appreciated,
> Thanks, Steve.



Tue, 30 Nov 2004 10:34:24 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing command line variables to AWK in shell script

2. Awk Command-line Variables - Thanks!

3. Command line variable assignment in awk

4. Command line variables to executables,command programs

5. Command line length - awk on a PC

6. Command-Line AWK

7. pass regexp to awk on command line

8. Command line options in awk scripts?

9. Help passing command line parameters to awk scripts

10. THANKS: Help passing command line parameters to awk script

11. awk print variable on a new line

12. Quoting Swamp: Saving an awk command in a shell variable

 

 
Powered by phpBB® Forum Software