----------------------------------------------------------------------------- Awk -- using arguments without reading them as files Read in the arguments using the ARGV variable but then you have to null out the elements of the ARGV array as you snarf them up. Otherwise nawk will try to open them as files when the main scanning loop begins. #!/bin/nawk -f BEGIN { awkoption = ARGV[1]; delete ARGV[1] } ... NOTE: if delete ARGV[1] fails try ARGV[1]="" or "/dev/null" --- Ken Manheimer --- klm@cme.nist.gov ----------------------------------------------------------------------------- awk two files but different actions This could be used to read a config or a table of data before the data to awk. Variable settings take effect when they are encountered on the command line, so, for example, you could instruct awk on how to behave for different files using this technique. For example: awk '{ program that depends on s }' s=1 file1 s=0 file2 Note that some versions of awk will cause variable settings encountered before any real filenames to take effect before the BEGIN block is executed, but some won't so neither way should be relied upon. ----------------------------------------------------------------------------- Arrays are possible in awk # found that bitmap[$0] would not work. # so assign to variables. coordx = $1 coordy = $2 # assign "X" to that element of array bitmap[coordx,coordy] = "X" ----------------------------------------------------------------------------- Reading from commands in awk The trick is to CLOSE the file that you use for the command pipe "date" | getline x; close "date"; ----------------------------------------------------------------------------- Getting a return interger awk 'END{ exit 7 }' value=$? echo value ----------------------------------------------------------------------------- Multiple Value returns from awk... To do this have awk output a shell script. You can then have awk define as many variables as you like. eval `awk ' END { print "FLAG="(NR > 0) ? 1 : 0 } ' -` if [ "$FLAG" = 1 ] ; then echo "FLAG=$FLAG" fi ----------------------------------------------------------------------------- Changing the record seperator inside an awk script. Awk reads and record separates its input before the script properly starts as such you need to get it to re-evaluate that input. For example... awk 'BEGIN { RS=";" } { $1=$1; printf "%s%1s\n",$0,";" } ' infile The $1=$1 is required to get awk to re-compute the $0 for the first record. ----------------------------------------------------------------------------- Determine which column numbers of relevant 'ps' fields CMDCOL=`ps -e | awk ' NR == 1 { for (i = 1; i <= NF; i++) if ($i == "COMMAND" || $i == "CMD" || $i == "COMD") cmdcol = i } END { print cmdcol } '` if [ "$CMDCOL" = "" ] then echo "$0: Unrecognised ps format for COMMAND field" exit 1 fi TTYCOL=`ps -e | awk ' NR == 1 { for (i = 1; i <= NF; i++) if ($i == "TTY") ttycol = i } END { print ttycol } '` if [ "$TTYCOL" = "" ] then echo "$0: Unrecognised ps format for TTY field" exit 1 fi # # Print list of all terminals running program # TTYLIST=`ps -e | awk ' $'"$CMDCOL"' == "vi" { print $'"$TTYCOL"' }'` echo Terminals running vi: echo $TTYLIST -----------------------------------------------------------------------------