Narly Nawk Script
Greg Hunt
hunt at dg-rtp.rtp.dg.com
Wed Dec 12 05:54:43 AEST 1990
In article <4255 at exodus.Eng.Sun.COM>, bm at bike2work.Eng.Sun.COM (Bill Michel) writes:
>
> I'm working on a shell script that makes extensive use of (n)awk.
> I'm *really confused as to the general workings of the script, and
> would apprecieate some help.
> Assume my script to be called "nawkfile" it is invoked as follows:
>
> nawkfile inputfile
>
> where inputfile is the file containing the input to be processed.
> My main questions are :
>
> 1) where does the data put into "string" go after the first call
> to nawk?
> 2) does $* mean a recursive call to the script, if so, how can
> this be used as input to the second nawk call
>
> The general layout is as follows
>
> nawk '
> {
> process a bunch of text, and append it to the variable "string"
> }
> END {
> print string
> }
> ' $* |
> nawk '
> {
> do some more processing
> }' |
>
[ rest of script deleted ]
1. The data put into "string" is being written (by the print command)
to the file descriptor called stdout (standard output). Using the
pipe symbol "|", you have told the shell to connect the stdout
from the first nawk to the stdin (standard input) of the second
nawk. So, the data is being written through the pipe to the
second nawk, which will use the data as input. Using a pipe to
do this is called "redirecting" the input and output.
2. The "$*" doesn't indicate a recursive call. It is the way you
get access to the command line arguments specified to the script.
Specifically, $* means "get me all of the command line arguments",
which in this case is only the name of the "inputfile". The
shell substitutes the arguments in place of the $*, so the first
nawk ends up being called with "inputfile" as its argument.
You can reference $* as many times as you care to in the script.
You can also get specific arguments by using $1, $2, etc. You
don't need to use the $* for the second nawk to get it's input,
you've already done that by using the pipe "|" from the first
nawk.
For more details on both of these points, you might want to look
at the man page for the shell (use "man sh | more"). It will tell
you about the various ways you can redirect input and output, and also
the ways you can manipulate shell script arguments.
Enjoy!
--
Greg Hunt Internet: hunt at dg-rtp.rtp.dg.com
DG/UX Kernel Development UUCP: {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA These opinions are mine, not DG's.
More information about the Comp.unix.questions
mailing list