#!/usr/bin/nawk -f # To run this script, type: nawk_intro.nawk # Do not include a file name or anything else after the command # Note: Esc [ H Esc [ J is the tput code to clear screen on Solaris BEGIN{print "Image this is a line from a data file:\n\n\n\n\n\n" print "The rain in Spain falls mainly on the plain!" } NR==1{printf"\n\n\n\nThis is how nawk interprets it:"} NR==2{printf"\n\n\n" print "+------------------- $0 -------------------+" print "| |" print "v v\n" print "The rain in Spain falls mainly on the plain!\n" print " ^ ^ ^ ^ ^ ^ ^ ^ ^" print " | | | | | | | | |" print "$1 $2 $3 $4 $5 $6 $7 $8 $9\n" } NR==3{printf"NAWK is a 'pattern { ACTION }' scripting language:"} NR==4{printf"\n where pattern can be specified as an " printf"extended Regular Expression" } NR==5{print " such as: /^[Tt]he [a-z]+ in Spain.*!$/"} NR==6{printf" pattern can also be specified as a condition: $4 == \"Spain\""} NR==7{printf"\n\n\nDo you think we can specify a pattern and no ACTION?"} NR==8{print " such as: /Spain/"} NR==9{printf"The default ACTION is to print the line: { print $0 }"} NR==10{print " resulting in: /Spain/ { print $0 }"} NR==11{printf"Do you think we can specify an { ACTION } only?"} NR==12{print " such as: { print $1 }"} NR==13{printf"{ ACTION } will apply to all lines to the file."} NR==14{print "\n" print " pattern { ACTION }\n" print " ^ ^" print " | |" print " +----- Rule -----+\n" } NR==15{printf"Here is a complete list of NAWK's built-in variables:"} NR==16{print "\n" print "NF - Number of fields in current record" print "RS - Input record separator (newline)" print "FS - Input field separator (spaces/tabs)" print "NR - Number of records from beginning of first input file" print "OFS - Output field separator (space)" print "ORS - Output record separator (newline)" print "FNR - Number of records from beginning of current file" print "OFMT - Output format for numbers (%.6g)" print "ARGC - Number of command line arguments" print "ARGV - Array of command line arguments" print "FILENAME - Current input filename" print "RSTART - Index into a string, set by match()" print "RLENGTH - Length of a substring, set by match()" print "CONVFMT - String conversion format for numbers (%.6g)" print "ENVIRON - Associative array for environment variables" print "SUBSEP - Separator character for array subscripts (\\034)\n" } NR==17{printf"For example, this one-liner interpolate's the shell's global " print "PATH variable:" } NR==18{print "nawk 'BEGIN{print ENVIRON[\"PATH\"]}'"} NR==19{printf"%s\n",ENVIRON["PATH"]} NR==20{printf"\nHere is how to find the many implementations of AWK:"} NR==21{print "" print "awk/nawk: ls /usr/bin/*awk*" print "gawk: http://www.gnu.org/software/gawk/gawk.html" print "GNU Awk User's Guide: http://www.gnu.org/manual/gawk/index.html" printf"Edition 3 of GAWK: Effective AWK Programming: " print "A User's Guide for GNU Awk:" print " http://www.gnu.org/manual/gawk-3.1.1/html_mono/gawk.html" print "tawk: http://www.tasoft.com/tawk.html" print "mawk (Mike Brennan awk): ftp://ftp.whidbey.net/pub/brennan" print "mksawk (part of the MKS Toolkit): http://www.mkssoftware.com/" print " http://www.mkssoftware.com/docs/man1/awk.1.asp" print " http://www.mkssoftware.com/docs/man1/awkc.1.asp" printf"awkcc (an awk compiler to C): previously at " print "http://www.unipress.com/att" print "awkc++: http://cm.bell-labs.com/cm/cs/who/bwk" print "awka: http://awka.sourceforge.net" print "awk2c: ftp://sunsite.unc.edu/pub/Linux/utils/text/awk2c050.tgz" print "a2p (awk to perl): ls /usr/perl5/bin/a2p" print "A great awk faq: http://www.faqs.org/faqs/computer-lang/awk/faq" printf"The GAWK pdf reference guide: " print "http://torvalds.cs.mtsu.edu/~neal/awkcard.pdf\n" } NR==22{printf"Here are the limitations to NAWK/AWK:"} NR==23{print " 100 fields" print " 2500 chars per input record" print " 2500 chars per output record" print " 1024 chars per field" print " 1024 chars per printf string" print " 400 chars max literal string" print " 400 chars in char class" print " 15 open files" print " 1 pipe" print " double-precision floating point" } NR==24{print "\n\nHappy Nawk scripting!\n"; exit} END{print "Warning:\n" print "too much NAWKing might make you into a NAWKaholic!\n" }