passing arguments to awk scripts

William R. Pringle wrp at PRC.Unisys.COM
Mon Mar 11 13:25:26 AEST 1991


In article <10094 at uwm.edu> datta at 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 at prc.unisys.com



More information about the Comp.unix.misc mailing list