AWK: Difference between revisions

From HaFrWiki42
Jump to navigation Jump to search
mNo edit summary
Line 13: Line 13:
# tawk, Thompson Automation, interpreter, compiler, MS-Windows DLL
# tawk, Thompson Automation, interpreter, compiler, MS-Windows DLL
# mksawk, Mortice Kern Systems (MKS), interpreter, compiler,  
# mksawk, Mortice Kern Systems (MKS), interpreter, compiler,  
== Examples ==
{| class="wikitableharm" width="1250"
! width="625" | Command
! width="625" | 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.
|-
| yes | head -28 | awk '{ print "hi" }'
| Prints 'hi' 28 times.
|-
| yes | head -4 | awk '{print rand()}'
| Prints 4 random numbers between 0 and 1. Always the same sequences. Use srand() to prevent this.
|}
{| class="wikitableharm" width="1250"
! width="625" | Scripts
! width="625" | Description
|-
|  { s += $1 } <br>END  { print "sum is", s, " average is", s/NR }
| Add up first column, print sum and average,
|-
| {line = $0} <br>END {print line}
| Prints the last line.
|-
| /Pattern/ {nlines = nlines + 1} <br>END {print nlines}
| Prints the total number of lines containing the word 'pattern'.
|-
|
|}


== See also ==
== See also ==
Line 20: Line 84:
* [http://www.pement.org/awk/awk1line.txt pement.org], AWK one liners by Eric Pement
* [http://www.pement.org/awk/awk1line.txt pement.org], AWK one liners by Eric Pement
* [http://www.ibm.com/developerworks/library/l-awk1 IBM], AWK Part 1.
* [http://www.ibm.com/developerworks/library/l-awk1 IBM], AWK Part 1.
* [tawk
*
    from Thompson Automation
    interpreter
    compiler
    MS-Windows DLL


== Reference ==
== Reference ==

Revision as of 16:53, 19 January 2015

AWK 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.


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'.


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 Part 1.

Reference

top