#! /bin/awk -f # ## Explore Lesser Known Features of AWK. # #Features explored # * Array variables and the command line # # * opening standard input from within the script. # # * discard arbitrary fields from a record # ## PD 2004 Manfred Waßmann http://www.berlinos.de/ ## # # # BEGIN{ # Set (additional) variables as if they were geiven on the command line ARGV[ARGC++]="t=42"; # This does NOT work for arrays: # # ARGV[ARGC++]="A[1]=42"; # # ARGV[ARGC++]="A=42"; # only because A is referenced as an array in END # Open stdin from within the script filename="elk.awk.input" ARGV[ARGC++]=filename; # create the file to be read system("ps ax >elk.awk.input"); } /elk.awk/{ print"Read line:\n"$0; # print matched line except second, third and fourth field $2=$3=$4="";print"mangled: "$0; } END{ # Output variables from the command line print"t = "t; for(i in A){print"A["i"] = "A[i];} # Clean up unless -vkeep=1 was given on the command line if(!keep){system("rm -f "filename);} }