A question on [a,ga,na]wk.

Harald Fuchs fuchs at it.uka.de
Sat Dec 1 04:57:47 AEST 1990


bt00 at 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 at it.uka.de> <fuchs%it.uka.de at relay.cs.net> ...
<fuchs at telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*



More information about the Comp.unix.shell mailing list