passing command line arguments to awk

Ben Cranston zben at umd5.UUCP
Fri May 30 08:39:13 AEST 1986


In article <198 at comp.lancs.ac.uk> craig at comp.lancs.ac.uk (Craig Wylie) writes:
>I surrender  --  is it possible to pass command line arguments to awk?
>If so - how.  While I'm at it how about doing the same thing to sed.

I'm not sure if this is really what you are asking.  You can use $n type
substitution if you use double-quotes to delimit the program; however;
beware that now all double-quotes and dollar signs in the program must 
be escaped with a backslash.  Consider the following shell script, which
looks a site up in the ARPA Internet site table:

#! /bin/sh
# host: Search InterNet host table for pattern
case $1 in
-a|-A) source=/u/netmap/hosts.txt; shift;;
*)     source=/u/netmap/lochosts.txt;;
esac
case $# in
0) echo 'Usage: host [-a] pattern' 1>&2; exit 2;;
esac
grep -i -e $1 <$source |
sed 's/ //g' |
awk -F: "
BEGIN { nf=0 }
\$1==\"HOST\" {
  if (\$4==\"\")
     \$4 = \"(Unknown)\"
  if (\$5==\"\")
     \$5 = \"(unknown)\"
  printf \"\n\"
  printf \"%s running %s with InterNet address %s\n\", \$4, \$5, \$2
  printf \"Names:    %s\n\", \$3
  printf \"Services: %s\n\", \$6
  nf += 1
}
END { 
  if (nf==0)
     print | \"echo $1 not found in $source 1>&2\" 
} " |
more -20

The substitution I am talking about is in the "if (nf==0)" clause at the
end - so it can say "luser.podunk.EDU not found" if it doesn't find it.

Is that what you wanted?
-- 
"We're taught to cherish what we have   |          Ben Cranston
 by what we have no longer..."          |          zben at umd2.umd.edu
                          ...{seismo!umcp-cs,ihnp4!rlgvax}!cvl!umd5!zben  



More information about the Comp.unix mailing list