To Index

 Documented in Volume 1 of the UNIX Programmers Manual.
 See /usr/doc/awk on 4.2 BSD systems for additional online documentation on this.


 % awk '{ printf "%s\n\n", $0 }' < infile
  doublespaces by listing to the standard output each line in infile followed
  by an extra newline.


 % awk -F\( '{print $1}' infile > outfile
  will take in input lines of:

      adb(1)	adb: debugger.
      acc(2)	acc: an example

  and strip the first field to become:

      adb
      acc

  et cetera...


 % awk -F\/ '$1 ~ /filename\$/ {n=NF-1} {print $(n)}' infile
  will take a list of filenames of the form /dir/dir1/.../filename
  from the file infile, and strip off the only the filename, and list
  it at the screen.

 From: david@ukma.UUCP (David Herron, NPR Lover)
 Subject: An example awk script

 #PROGRAM: avg.awk - Average the columns of a table of numbers.
 # The table may have varying numbers of fields, any number of fields,
 # or any number of records.  Missing fields are assumed to be 0.
 #
 # Usage:
 #	awk -f avg.awk out
 #
 # It prints a single line having one number per field, which is the
 # average for that field over the whole table.
 #
 # Author:
 #	David Herron	(ukma!david)
 #	University of Kentucky
 #
 BEGIN	{
 		maxnf = 0;
 	}
 	{
 		for (i=1; i<=NF; i++) {
 			tab[i] = tab[i] + $(i);
 		}
 		if (NF > maxnf)
 			maxnf = NF;
 	}
 END	{
 		for (i=1; i<=maxnf; i++)
 			printf " %d ",(tab[i]/NR);
 		printf "\n";
 	}

 % awk '{x1 = substr($0, 48, 9); printf("%s\n",x1)}' infile
 will list columns 48-56 on stdout. Here's how it works:
 for each line in the file 'infile' in the current directory,
 take the substring of the entire line ($0) which starts at column 48
 and is nine columns long.  Set the variable x1 equal to it.
   Print to stdout with printf the string in x1 (%s) followed by a
 newline (\n).

 From: Kathleen G. Kopley 

 % awk -F: '{if ($1 != "") print$1}' xin > xout
 will read the file xin in the current directory, and eliminate blank lines
 and store the result into file xout in the current directory.


 #! /bin/awk -f
 #PROGRAM: turns all (and only) lowercase characters into uppercase.
 BEGIN {
 	# load letters into array A
 	alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 		for ( i=1 ; i<=length(alpha) ; i++ ) {
 		A[i]=substr(alpha,i,1);
 		}
 	}
 	{
 	nline="";				# initialize the output line
 	for ( i=1 ; i<=length($0) ; i++ ) {	# process each line
 		ch=substr($0,i,1);		# process a character
 		if (ch>="a" && ch<="z") {	# select lowercase only
 			c=index(alpha,ch)+26;	# get corresponding index
 			ch=A[c];		# get corresponding uppercase
 			}
 		nline=(nline ch);		# accumulate characters
 		}				# done with each line
 	 printf("%s\n",nline);			# put line to stdout
 	}
 # The tr command will do this, too.



 #PROGRAM: Print lines longer than 72 characters:
 length > 72



 #PROGRAM: Print first two fields in opposite order:
 { print $2, $1 }



 #PROGRAM: Add up first column, print sum and average:
 { s += $1 }
 END  { print "sum is", s, " average is", s/NR }



 #PROGRAM: Print fields in reverse order:
 { for (i = NF; i > 0; --i) print $i }



 #PROGRAM: Print all lines between start/stop pairs:
 /start/, /stop/



 #PROGRAM: Print all lines whose first field is different
 #         from previous one:
 $1 != prev { print; prev = $1 }



 #PROGRAM: pad all lines to at least 80 wide
 {
 x1 = $0				# capture the input line in x1
 for ( i=length($0) ; i<80; i++)	# pad short lines out to 80
  {
  x1=(x1 " ")			# line length < 80 so append a blank
  }
 printf("%s\n",x1)		# all done
 }



 #PROGRAM: count the number of lines.
 END	{print NR}



 #PROGRAM: print all lines containing 'doug'.
 /doug/



 #PROGRAM: print all lines containing 'doug', 'ken', or 'dmr'.
 /ken|doug|dmr/



 #PROGRAM: print the third field of each line.
 {print $3}



 #PROGRAM: print the third and second fields of each line, in that order.
 {print $3, $2}



 #PROGRAM: append all lines containing 'doug', 'keen', and 'dmr1'.
 #         to files 'jdoug', 'jkeen', and 'jdmr1', respectively.
 /keen/	{print >"jkeen"}
 /doug/	{print >"jdoug"}
 /dmr1/	{print >"jdmr1"}



 #PROGRAM: print each line prefixed by 'line-number\ :\ '.
 {print NR ": " $0}



 #PROGRAM: sum the fourth column of a table.
 {sum = sum + $4}
 END	{print sum}


 From: pdbain@wateng.UUCP (Peter Bain) Organization: U of Waterloo, Ontario
 Subject: refer(1) formatter

  This is an awk(1) script to convert refer(1) format data to a more
 human-readable form. Of course, running refer-format stuff through
 refer and troff(1) will do the same thing (and much better), but this works
 faster.

 #PROGRAM: Convert refer format data to human readable form.
 /^%/ {
 	prevfield = curfield
 	curfield = substr($0,2,1)
 	if (curfield == "m")
 		curfield = "M"
 	if (fields[curfield])
		fields[curfield] = fields[curfield] ", " substr($0,4)
	else
		fields[curfield] = substr($0,4)
	}

 /^[^%]/ {
 	fields[curfield] = fields[curfield] "\n" $0
 	}


 /^$/	{
	doc = ""
	if (fields["A"]) {
		doc = doc fields["A"]
		if (fields["l"] ~ /edited.*/ && !fields["E"])
			if (fields["e"])
				doc = doc "(eds.)"
			else
				doc = doc "(ed.)"
		doc = doc ","
	}
	if (fields["T"])
		if (fields["B"] || fields["J"]) {
			if (length(doc fields["T"]) > 73) {
				print doc
				doc = ""
			}
			doc = doc "\"" fields["T"] "\", "
		} else {
			if (length(doc fields["T"]) > 73) {
				print doc
				doc = ""
			}
			doc = doc fields["T"] ", "
		}
	if (fields["J"]) {
		if (length(doc fields["J"]) > 73) {
			print doc
			doc = ""
		}
		doc = doc fields["J"] ", "
		if (fields["V"] ) {
			if (fields["N"]) {
				volno = fields["V"] "(" fields["N"] ")"
				if (length(doc volno) > 73) {
					print doc
					doc = ""
				}
			} else
				volno = "vol. " fields["V"]
			doc = doc volno ", "
			volno = ""
		}
	}
	if (fields["M"])
		date = fields["M"]
	if (fields["D"])
		date = date " "fields["D"]
	if (date) {
		if (length(doc date) > 73) {
			print doc
			doc = ""
		}
		doc = doc date ", "
		date = ""
	}
	if (fields["E"]) {
		if (length(doc fields["E"]) > 73) {
			print doc
			doc = ""
		}
		doc = doc fields["E"]
		if (fields["e"])
			doc = doc "(eds.)"
		doc = doc ", "
	}
	if (fields["l"] == "dissertation") {
		if (fields["Q"]) {
			level = fields["Q"] " dissertation,"
			if (length(doc level) >73) {
				print doc
				doc = ""
			}
			doc = doc level
		}
	}
	if (fields["I"]) {
		if (length(doc fields["I"]) >73) {
			print doc
			doc = ""
		}
		doc = doc fields["I"] ", "
	}
	if (fields["S"]) {
		if (length(doc fields["S"]) >73) {
			print doc
			doc = ""
		}
		doc = doc fields["S"] ", "
	}
	if (fields["C"]) {
		if (length(doc fields["C"]) >73) {
			print doc
			doc = ""
		}
		doc = doc fields["C"] ", "
	}
	if (fields["P"]) {
		if (length(doc fields["P"]) >73) {
			print doc
			doc = ""
		}
		doc = doc fields["P"] ", "
	}
	print doc "\n"
	for (i in fields)
		fields[i] = ""
	}



 Punctuation:

 1	!	logical not
 2	!=	not equal to
 3	!~	does not match your regular expression
 4	~	does match your regular expression
 5	"	string literal delimiter
 6	#	comment the rest of a source line
 7	%	modulo
 8	%=	modulo and then assign
 9	&&	logical and
 10	(...)	denotes for and while loops; groups concatenations and patterns
 11	*	multiplication operator
 12	*=	multiply and then assign
 13	+	addition operator
 14	++	increment
 15	+=	increment and then assign
 16	,	separates a set of patterns
 17	-	subtraction operator
 18	--	decrement
 19	-=	decrement and then assign
 20	/	division operator
 21	/=	divide and then assign
 22	;	statement separator
 23	<	less than
 24	<=	less than or equal to
 25	=	simple assignment operator
 26	==	is equal to
 27	>	greater than
 28	>	writes out to specified file
 29	>=	greater than or equal to
 30	>>	appends to specified file
 31	[...]	denotes an subscript of an array; arrays are declared implicitly
 32	{...}	specifies and delimits an action
 33	|	pass to a unix filter
 34	||	logical or

 Predefined variables:

	FILENAME	name of the current filename
	$0		the entire current record
	$1,$2,...	field 1, field 2, ...
	prev		the previous record
	BEGIN		to perform before any records are read (must be 1st pat)
	END		to perform after all records are read (must be last pat)
	NR		ordinal number of current record
	NF		number of fields in current record
	FS		input field separator, default blank
	OFS		output field separator, default blank
	RS		input record separator, default newline
	ORS		output record separator, default newline
	OFMT		output format for numbers, default "%.6"

 Loop structures:

	if(NF>maxnf) {statement; statement}
	for(1=1;i<=NF;i++) {statement; statement}
	while(i<=NF) {statement; statement}
	next		skip to the next record and process from the top
	break		exit from enclosing while or for
	continue	cause the next iteration to begin
	exit		behave as though end-of-input has occurred

 Builtin Functions:

	exp(x)		returns exponent of x
	index($0,"a")	returns the column where "a" is within the line($0)
	int(x)		returns integer part of x
	length(x)	returns the length of x
	log(x)		returns logarithm of x
	print $1	prints variable $1
	print | unixcmd	pipes the current record to unixcmd
	printf(...)	as in C, list to stdout
	sprintf(...)	as in C, return the result as a string
	split(s,ar,sep)	split array s into ar[1]...ar[n] using sep as delimiter
	sqrt(x)		returns squareroot of x
	substr($0,48,9)	returns string of 9 chars, from cc 48-56, of string $0
	substr(...)	NOTE: will fail except on simple assignments

 Special-case constructs:

	BEGIN { FS = ":" }	sets ':' as the field separator
	a1="string"	assigns the string a1 to be 'string'
	a1=(a1 a1)	concatenates a1 to itself (note the space separator)
	x[NR]=$0	assign current line to NR'th element of array x.
	for(i in array)	set i in turn to each element of array
	pat1, pat2 {..}	invokes {..} between occurrences of pat1 and pat2
	n1=(n1 "")	forces numeric variable n1 to become a string
	s1=s1 + 0	forces string s1 to become numeric

 BUGS: Awk's version of 'printf' core dumps or prints garbage when a field
 width variable is used....

 Named for its authors: Aho, Weinberger, and Kernighan.

To Index