[comp.unix.misc] passing arguments to awk scripts

datta@vacs.uwp.wisc.edu (David Datta) (03/10/91)

Can it be done? Is there a way I can address a 
command line argument within the script?


--
-Dave datta@vacs.uwp.edu [....uwm!uwpvacs!datta] Are you a lemming?
Lemmings jump off cliffs every four years. There are no five year old
lemmings. Unless they've learned to think for themselves.

tchrist@convex.COM (Tom Christiansen) (03/10/91)

From the keyboard of datta@vacs.uwp.wisc.edu (David Datta):
:Can it be done? Is there a way I can address a 
:command line argument within the script?

1. Sure -- just run it through the awk-to-perl translator. 

2. Actually, gawk and nawk will also let you get at them, although
   awk won't.  Check your man page; if all you have is awk, return
   to step one.  You might even find some other features there. :-)

--tom

datta@vacs.uwp.wisc.edu (David Datta) (03/11/91)

In tchrist@convex.COM (Tom Christiansen) sort-of writes:

>From the keyboard of datta@vacs.uwp.wisc.edu (David Datta):
>:Can it be done? Is there a way I can address a 
>:command line argument within the script?

>   Actually, gawk and nawk will also let you get at them, although
>   awk won't.  Check your man page
>--tom

I started with the man pages they make NO MENTION of parameter passing
BUT:

From: dwn@swbatl.sbc.com (David Neill)

If you want to pass variables to awk, it works like so:

awk '
{ script stuff using VAR1 and VAR2 }
' VAR1=something VAR2=somethingelse filenames

THANKS Dave!

For example, with the file "script"

-----------TOF
{
print var1
}
EOF-----------

calling 

awk -f script var1="hi"

will work.
--
-Dave datta@vacs.uwp.edu [....uwm!uwpvacs!datta] The moon goes around in
 orbit.  It's the only place the moon does go.  The moon only makes left
 turns.  To get anywhere else you have to get off.  WANT TO GET OFF?

wrp@PRC.Unisys.COM (William R. Pringle) (03/11/91)

In article <10094@uwm.edu> datta@vacs.uwp.wisc.edu (David Datta) writes:
>Can it be done? Is there a way I can address a 
>command line argument within the script?

Awk (nawk, gawk, etc.) accept arguments as keyword-value pairs when
entered from the command line.  For example, to pass the current directory
to awk, type something like: PWD=`pwd`, for your home directory,
HOME=${HOME}, etc.

For example, the following file (asdf):

#!/bin/sh

awk '{printf "Line %d of %s/%s: >>%s<<\n", NR, Pwd, FILENAME, $0}' \
	Pwd=`pwd` asdf

produces the following output:
Line 1 of /guests/wrp/asdf: >>#!/bin/sh<<
Line 2 of /guests/wrp/asdf: >><<
Line 3 of /guests/wrp/asdf: >>awk '{printf "Line %d of %s/%s: >>%s<<\n", NR, Pwd, FILENAME, $0}' \<<
Line 4 of /guests/wrp/asdf: >>	Pwd=`pwd` asdf<<

Bill Pringle
wrp@prc.unisys.com

steinbac@hpl-opus.hpl.hp.com (Guenter Steinbach) (03/12/91)

In nawk you can access ARGV and ARGC similar to C.

Get the book "The AWK programming language" by Aho, Kernighan (sp?) and
Weinberger themselves.  Here is a piece of a script that I lifted
from the book, it should give you an idea of how this works:

nawk 'BEGIN {						# deal with args
	    for(i=1;ARGV[i] ~ /^[0-9]+$/;i++) {		# collect numbers
		fld[++nf] = ARGV[i]			# into array fld
		ARGV[i] = ""				# remove from ARGV
		}
	    if(i >= ARGC) ARGV[ARGC++] = "-"		# no file name: stdin
	    }						# end of BEGIN

    at this point, only file names or "-" are left in ARGV, and
    any integer number arguments are collected in fld.

Good luck.

	 Guenter Steinbach		gunter_steinbach@hplabs.hp.com