[comp.unix.shell] A question on [a,ga,na]wk.

bt00@PL118d.? (Binod K. Taterway) (11/28/90)

Is it possible to make use of c-shell variables in [g,n]awk scripts.
What I need to do some thing like this:

  #!/bin/csh
  set AWK=/usr/local/bin/gawk
  set date=`date +%m/%d/%y`
  # Now the awk script that uses $date:
  $AWK -F: '{if ($3 == $date) {print $0}}' <source-file>
  # End of shell script.

I checked  with [g,n]awk. Only gawk  can access environment variables,
but I do not want to  do  "setenv date= ..",  just "set date= ..". Any
suggestions? Or, am I stuck with using setenv?

Thanks in advance.

/Binod.
--------------------
Binod Taterway		bt00@pl118a.cc.lehigh.edu | bt00@lehigh.bitnet
User Consultant, Lehigh University, Bethlehem, PA 18015	(215) 758-3984

fuchs@it.uka.de (Harald Fuchs) (12/01/90)

bt00@PL118d.? (Binod K. Taterway) writes:
>Is it possible to make use of c-shell variables in [g,n]awk scripts.
>What I need to do some thing like this:

>  #!/bin/csh
>  set AWK=/usr/local/bin/gawk
>  set date=`date +%m/%d/%y`
>  # Now the awk script that uses $date:
>  $AWK -F: '{if ($3 == $date) {print $0}}' <source-file>
>  # End of shell script.

>I checked  with [g,n]awk. Only gawk  can access environment variables,
>but I do not want to  do  "setenv date= ..",  just "set date= ..". Any
>suggestions? Or, am I stuck with using setenv?

Solution 1, works for gawk and maybe for nawk:
Use the -v flag
  #!/bin/csh
  set AWK=/usr/local/bin/gawk
  set date=`date +%m/%d/%y`
  # Now the awk script that uses $date:
  $AWK -v date=$date -F: '{if ($3 == date) {print $0}}' <source-file>
  # End of shell script.

Solution 2, works for every awk:
Take $date out of the single quotes
  #!/bin/csh
  set AWK=/usr/local/bin/gawk
  set date=`date +%m/%d/%y`
  # Now the awk script that uses $date:
  $AWK -F: '{if ($3 == '"$date"') {print $0}}' <source-file>
  # End of shell script.
The double quotes are not necessary, but they will be if $date contains
shell-metacharacters (e.g. spaces).

BTW: shouldn't your subject line say
   A question on {a,ga,na}wk\.    # :-)
--

Harald Fuchs <fuchs@it.uka.de> <fuchs%it.uka.de@relay.cs.net> ...
<fuchs@telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*

arnold@audiofax.com (Arnold Robbins) (12/01/90)

In article <BT00.90Nov28155200@PL118d.?> bt00@PL118d.? (Binod K. Taterway) writes:
>Is it possible to make use of c-shell variables in [g,n]awk scripts.
>What I need to do some thing like this:
>
>  #!/bin/csh
>  set AWK=/usr/local/bin/gawk
>  set date=`date +%m/%d/%y`
>  # Now the awk script that uses $date:
>  $AWK -F: '{if ($3 == $date) {print $0}}' <source-file>
>  # End of shell script.

You can do variable assignments to awk variables on the command line.
The behaviour is most consistent with gawk and very recent versions of
nawk, but this should work with all awk variants:

	set date=`date +%m/%d/%y`
	$AWK -F: '{if ($3 == date) {print $0}}' date="$date" <source-file>

Of course, your awk program might better be written as

	$AWK -F: '$3 == date' date="$date" <source-file>

See the gawk manual for full details on the way the awk command line works.
-- 
Arnold Robbins				AudioFAX, Inc. | Laundry increases
2000 Powers Ferry Road, #200 / Marietta, GA. 30067     | exponentially in the
INTERNET: arnold@audiofax.com Phone:   +1 404 933 7612 | number of children.
UUCP:	  emory!audfax!arnold Fax-box: +1 404 618 4581 |   -- Miriam Robbins

stuart@siesoft.co.uk (12/08/90)

fuchs@it.uka.de (Harald Fuchs) writes:

>Solution 2, works for every awk:
>Take $date out of the single quotes
>  #!/bin/csh
>  set AWK=/usr/local/bin/gawk
>  set date=`date +%m/%d/%y`
>  # Now the awk script that uses $date:
>  $AWK -F: '{if ($3 == '"$date"') {print $0}}' <source-file>
                        ^^^^^^^^^
>  # End of shell script.
>The double quotes are not necessary, but they will be if $date contains
>shell-metacharacters (e.g. spaces).

My first reaction to this was that the double quotes are always necessary
because otherwise awk treats it as an awk variable (if $data were "jan",
say, then awk would try and match $1 with the contents of jan).  I think
this is true on most (if not all) versions of awk.

There is however another mistake in that the single and double quotes are
the wrong way round, you are telling awk that $date is a string, not csh.
So it should be:

...
$AWK -F: '{if ($3 == "'$date'") {print $0}}' <source-file>
...

>Harald Fuchs <fuchs@it.uka.de> <fuchs%it.uka.de@relay.cs.net> ...
><fuchs@telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*

Stuart.
-
--
S I E M E N S  Stuart Hood 65-73 Crockhamwell Road, Woodley, Berkshire, UK
-------------  Phone: + 44-734-691994          Email: stuart@siesoft.co.uk
N I X D O R F  The trouble with everyone, is that they generalise too much

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (12/09/90)

  I missed the first part of this, but in general you can't use shell
variables in ' quotes because they don't get expanded.

This fails:

	awk '/$1 == $date/{ print $4 }' file

These work:
	awk "/\$1 == $date/{ print \$4 }" file
	awk '/$1 == date/{ print $4}' date=$date file

  The first allows substitution of the $date, but uses escapes to pass
the $ to awk (and you need escapes for any " quotes, too. The second is
the better way to do it, by defining an awk variable on the command
line.

NOTE: some versions of awk will not accept command line definitions
unless there is a filename. If you are reading stdin you must put a - in
for the filename:
	something | awk '/$1 == date/{ print $4}' date=$date -

  I hope that's what people were trying to say, the original question is
either behind the answers or very old. I have 10 days spooled and I
don't have it.
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

walter@garth.UUCP (Walter Bays) (12/14/90)

In article <BT00.90Nov28155200@PL118d.?> bt00@PL118d.? (Binod K. Taterway) writes:
>Is it possible to make use of c-shell variables in [g,n]awk scripts.

I often do it like...

#!/bin/ksh
Tempfile="c4sim.tmp$$"
...
nawk '
    ...
    print ... >$Tempfile
    ...
    ' "Tempfile=$Tempfile" "Version=$Version" $*

---
Walter Bays		Phone (415) 852-2384	FAX (415) 856-9224
EMAIL walter@apd.ingr.com
USPS: Intergraph APD, 2400 Geng Road, Palo Alto, California 94303
-- 
Walter Bays		Phone (415) 852-2384	FAX (415) 856-9224
EMAIL walter@apd.ingr.com	or	...!ingr!apd!walter
USPS: Intergraph APD, 2400 Geng Road, Palo Alto, California 94303