Awk variable woes 
Author Message
 Awk variable woes

I am trying to put a variable in
awk '{ print $0; if ( $0 ~ /$VARIABLEHERE/) print "" }' somefile.tmp

When I put in the exact word such as /CONTINUE/ it will work, but when I
try to use a variable as above it does not work.  I am trying to add a line
to a text
file if it comes across different words (notified by the $VARIABLEHERE)
any ideas?

Thanks,
Adam



Sun, 04 Nov 2001 03:00:00 GMT  
 Awk variable woes
Quote:

>I am trying to put a variable in
>awk '{ print $0; if ( $0 ~ /$VARIABLEHERE/) print "" }' somefile.tmp

Try this:
awk -v VARIABLE=varable_value '{ print $0;
       if ( $0 ~ /$VARIABLE/) print "" }' somefile.tmp
or:
awk -v VARIABLE=varable_value '{ print $0;
       if ( $0 ~ /VARIABLE/) print "" }' somefile.tmp

Good luck

marc

Quote:

>When I put in the exact word such as /CONTINUE/ it will work, but when I
>try to use a variable as above it does not work.  I am trying to add a line
>to a text
>file if it comes across different words (notified by the $VARIABLEHERE)
>any ideas?

>Thanks,
>Adam




Sun, 04 Nov 2001 03:00:00 GMT  
 Awk variable woes

Quote:

> awk '{ print $0; if ( $0 ~ /$VARIABLEHERE/) print "" }' somefile.tmp

You commandline looks like you are trying to refer to a shell variable
inside your awk program. In other words: between the apostrophes you are
in awk space, not in shell space.

If your awk knows about the ENVIRON array (gawk does), you can use

  awk '{ print $0; if ( $0 ~ ENVIRON["VARIABLEHERE"] print "" }' somefile.tmp

Otherwise you probably have to fiddle with the quoting/escaping
mechanisms of your particular shell. I tried to get that correct with
bash, but obviously I'm too dumb for that.

Another way of passing a variable down into your awk program is provided
by gawk's `-v' options, I don't know if other awks have that switch,
too, e.g.:

  awk -v AwkVariable=$SHELL_VARIABLE '{ print $0; if ( $0 ~ AwkVariable) print "" }' somefile.tmp

Regards...
        Michael



Sun, 04 Nov 2001 03:00:00 GMT  
 Awk variable woes

Quote:


> > awk '{ print $0; if ( $0 ~ /$VARIABLEHERE/) print "" }' somefile.tmp

> You commandline looks like you are trying to refer to a shell variable
> inside your awk program. In other words: between the apostrophes you are
> in awk space, not in shell space.

> If your awk knows about the ENVIRON array (gawk does), you can use

>   awk '{ print $0; if ( $0 ~ ENVIRON["VARIABLEHERE"] print "" }' somefile.tmp

> Otherwise you probably have to fiddle with the quoting/escaping
> mechanisms of your particular shell. I tried to get that correct with
> bash, but obviously I'm too dumb for that.

> Another way of passing a variable down into your awk program is provided
> by gawk's `-v' options, I don't know if other awks have that switch,
> too, e.g.:

Brian Kernighan's AWK  supports -v as does mawk (I think).

Quote:
>   awk -v AwkVariable=$SHELL_VARIABLE '{ print $0; if ( $0 ~ AwkVariable) print "" }' somefile.tmp

> Regards...
>         Michael

Peter
--


Opinions expressed are my own and not necessarily those of my employer.


Mon, 05 Nov 2001 03:00:00 GMT  
 Awk variable woes

Quote:



>> > awk '{ print $0; if ( $0 ~ /$VARIABLEHERE/) print "" }' somefile.tmp

>> You commandline looks like you are trying to refer to a shell variable
>> inside your awk program. In other words: between the apostrophes you are
>> in awk space, not in shell space.

>> If your awk knows about the ENVIRON array (gawk does), you can use

>>   awk '{ print $0; if ( $0 ~ ENVIRON["VARIABLEHERE"] print "" }' somefile.tmp

>> Otherwise you probably have to fiddle with the quoting/escaping
>> mechanisms of your particular shell. I tried to get that correct with
>> bash, but obviously I'm too dumb for that.

>> Another way of passing a variable down into your awk program is provided
>> by gawk's `-v' options, I don't know if other awks have that switch,
>> too, e.g.:

>Brian Kernighan's AWK  supports -v as does mawk (I think).

The new version aka nawk does the default solaris awk is the old version,
see below:
caps-he1$ type awk
awk is /bin/awk
caps-he1$ awk -v fred="FRED123" ' BEGIN {print fred}'
awk: syntax error near line 1
awk: bailing out near line 1
caps-he1$ awk -v fred="FRED123" ' {print fred}'
awk: syntax error near line 1
awk: bailing out near line 1
caps-he1$ awk  fred="FRED123" ' {print fred}'
awk: syntax error near line 1
awk: bailing out near line 1
caps-he1$ awk   ' {print fred}'
caps-he1$ awk   'BEGIN {print fred}'

caps-he1$ nawk -v fred="FRED123" ' BEGIN {print fred}'
FRED123
caps-he1$ uname -a
SunOS caps-he1 5.6 Generic_105181-04 sun4u sparc SUNW,Ultra-4
caps-he1$

The current standard awk's are all based on nawk/posix as far as I know
(hpux,linux,openbsd,freebsd) except for solaris. then it is nawk go figure.

marc

- Show quoted text -

Quote:

>>   awk -v AwkVariable=$SHELL_VARIABLE '{ print $0; if ( $0 ~ AwkVariable) print "" }' somefile.tmp

>> Regards...
>>         Michael

>Peter
>--


>Opinions expressed are my own and not necessarily those of my employer.



Mon, 05 Nov 2001 03:00:00 GMT  
 Awk variable woes

Quote:

> I am trying to put a variable in
> awk '{ print $0; if ( $0 ~ /$VARIABLEHERE/) print "" }' somefile.tmp

Extrapolate from this example (and _carefully_ note the quoting):

        awk '{ if ($0 ~ /'"$LOGNAME"'/) print $0 }' /etc/passwd

--
========================================================================

      http://www.oldradio.com/archives/nelson/open-computing.html
``Those who don't understand UNIX are condemned to reinvent it, poorly.''



Wed, 14 Nov 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Awk woes... - assn1a.gif (0/1)

2. awk variables --> shell variables

3. tcl 8.4 namespace variable woes (maybe BLT 2.4z as well)

4. How do I display the contents of a shell variable from an awk script via a awk variable

5. variabe in variable like in ksh in awk?

6. AWK-Script to set a shell variable (Win32) ?

7. Passing command line variables to AWK in shell script

8. awk variables and ``string

9. Awk Command-line Variables - Thanks!

10. Awk Command-Line Variables

11. awk print variable on a new line

12. Creating variables using awk

 

 
Powered by phpBB® Forum Software