🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
4 messages
4 total messages Started by Binod K. Taterway Wed, 28 Nov 1990 15:53
A question on [a,ga,na]wk.
#1869
Author: Binod K. Taterway
Date: Wed, 28 Nov 1990 15:53
20 lines
1210 bytes
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
Re: A question on [a,ga,na]wk.
#1870
Author: Harald Fuchs
Date: Fri, 30 Nov 1990 17:57
41 lines
1992 bytes
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*
Re: A question on [a,ga,na]wk.
#1907
Author: stuart
Date: Fri, 07 Dec 1990 17:21
36 lines
1900 bytes
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
Re: A question on [a,ga,na]wk.
#1915
Author: Wm E. Davidsen Jr
Date: Sun, 09 Dec 1990 02:29
30 lines
1708 bytes
  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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads