AWK

From HaFrWiki42
Revision as of 19:19, 23 January 2015 by Hjmf (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

AWK [1] is an extremely versatile programming language for working on files and is named after its three original authors:

  • Alfred V. Aho
  • Brian W. Kernighan
  • Peter J. Weinberger

They write: Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data-manipulation tasks.

Most implementations of awk are interpreters which read your awk source program and parse it and act on it directly. Example implementations:

  1. nawk, AT&T's `new awk' -- probably nobody uses the `old awk' anymore-- . Interpreter, might NOT be well-maintained
  2. gawk, GNU project, interpreter.
  3. mawk, Michael Brennan, interpreter.
  4. tawk, Thompson Automation, interpreter, compiler, MS-Windows DLL
  5. mksawk, Mortice Kern Systems (MKS), interpreter, compiler,

Examples

Command Description
awk '{ print $2, $1 }' file Print first two fields in opposite order.
awk 'length > 80' file Print lines longer than 80 characters.
awk '{print length($2)}' file Print length of string in 2nd column.
awk '{ for (i = NF; i > 0; --i) print $i }' file Print fields in reverse order.
awk '/start/, /stop/' file Prints all lines between start/stop pairs including 'start' and 'stop'.
awk '$1 != prev { print; prev = $1 }' file Prints all lines whose first field is different from previous one.
awk '$1 > $2 {print $3}' file Print column 3 of column 1 > column 2.
awk '$3 > $2' file Prints line if column 3 > column 2.
awk '$3 > $1 {print i + "1"; i++}' file Counts number of lines where column 3 > column 1
awk '{print NR, $1}' file Prints sequence number and column 1 of file.
awk '{$2 = ""; print}' file Prints every line after erasing the 2nd column.
head -28 | awk '{ print "hi" }' Prints 'hi' 28 times.
head -4 | awk '{print rand()}' Prints 4 random numbers between 0 and 1. Always the same sequences. Use srand() to prevent this.
awk '{printf("%15s %12s %3s %12s %12s %-s\n", $5, $1, $2, $3, $4, $9)}' | sort Sorts a directory on size and show the result.
df -P / | grep /dev | awk '{print $5}' | sed 's/%//g' Shows the free capacity (which is a percentage) of the root directory of a hard disk


Scripts Description
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
Add up first column, print sum and average,
{line = $0}
END {print line}
Prints the last line.
/Pattern/ {nlines = nlines + 1}
END {print nlines}
Prints the total number of lines containing the word 'pattern'.

GAWK

The GAWK implementation is also available for windows.

See also

top

  • Grymoire, Tutorial and introduction made by Bruce Barnet.
  • GNU.org, The GNU Awk User’s Guide.
  • pement.org, AWK one liners by Eric Pement
  • IBM, AWK Tutorial Part 1.
  • IBM, AWK Tutorial Part 2.

Reference

top

  1. AWK, Wikipedia AWK article.