passing command line arguments to awk

David P Huelsbeck dph at lanl.ARPA
Mon Sep 22 19:48:21 AEST 1986


In article <731 at scc.UUCP> steiny at scc.UUCP (Don Steiny) writes:
>In article <753 at moscom.UUCP>, de at moscom.UUCP (Dave Esan) writes:
>> > 
>> > I surrender  --  is it possible to pass command line arguments to awk?
>> > If so - how.  
>
>	awk ' statement ; . . . ' file
>
>be sure and use single quotes!   Lots of awk characters have
>special meaning to the shell.  The following prints the uids in
>the password file from highest to lowest:
>
>	awk -F: '{ print $3 }' | sort -n -r
>
>
>-- 
>scc!steiny
>Don Steiny @ Don Steiny Software 
>109 Torrey Pine Terrace
>Santa Cruz, Calif. 95060
>(408) 425-0382


This is a special case but an important one. Using the -F<c> option sets the
field separator to the character <c>. This is important because setting the
field separator with:

	awk '{ print $3 }' FS=\":\" | sort -n -r

will not set the FS variable to ":". (at least on 4.2) I haven't checked but
I suspect that it's caused by the fact that parameter values passed from the 
command line are not made available until after the first record has been read
and parsed. Other variables not used in parsing (such as OFS etc.) may be set
in the way I've shown. In the case of RS I know of no way to set it from the
command line.

	dph at lanl.arpa



More information about the Comp.unix mailing list