AWK Source Code Blocks in Org Mode
Org Mode support for AWK
Introduction
AWK
is a task-specific language optimized for regular expression
record data manipulation. It is a little language that is Turing
complete, efficient to execute, reasonably simple to express, powerful
within its scope, and pathologically cryptic.
A natural Org mode representation of the record data AWK
was
designed to manipulate is the table.
Requirements and Setup
There are many well-maintained AWK compatible languages. The
variable org-babel-awk-command
holds the name of AWK
executable.
The default is "awk".
To configure AWK
source code blocks in Org mode, add the appropriate
dotted pair to org-babel-load-languages
(org-babel-do-load-languages 'org-babel-load-languages '((awk . t)))
Org Mode Features for AWK Source Code Blocks
Header Arguments
There are three AWK
-specific header arguments.
:cmd-line
- takes command line arguments to pass to the
AWK
executable :in-file
- takes a path to a file of data to be processed by
AWK
:stdin
- takes an Org-mode data or code block reference, the
value of which will be passed to the
AWK
process through STDIN
Sessions
AWK
does not support sessions.
Examples of Use
Two examples from GNU Awk User's Guide show simple uses of AWK
.
Given the table bbs-list
aardvark | 555-5553 | 1200/300 | B |
alpo-net | 555-3412 | 2400/1200/300 | A |
barfly | 555-7685 | 1200/300 | A |
bites | 555-1675 | 2400/1200/300 | A |
camelot | 555-0542 | 300 | C |
core | 555-2912 | 1200/300 | C |
fooey | 555-1234 | 2400/1200/300 | B |
foot | 555-6699 | 1200/300 | B |
macfoo | 555-6480 | 1200/300 | A |
sdace | 555-3430 | 2400/1200/300 | A |
sabafoo | 555-2127 | 1200/300 | C |
this AWK
source code block
#+begin_src awk :stdin bbs-list /foo/ { print $0 } #+end_src
returns a subset of the original table
fooey | 555-1234 | 2400/1200/300 | B |
foot | 555-6699 | 1200/300 | B |
macfoo | 555-6480 | 1200/300 | A |
sabafoo | 555-2127 | 1200/300 | C |
Given the table inventory-shipped
Jan | 13 | 25 | 15 | 115 |
Feb | 15 | 32 | 24 | 226 |
Mar | 15 | 24 | 34 | 228 |
Apr | 31 | 52 | 63 | 420 |
May | 16 | 34 | 29 | 208 |
Jun | 31 | 42 | 75 | 492 |
Jul | 24 | 34 | 67 | 436 |
Aug | 15 | 34 | 47 | 316 |
Sep | 13 | 55 | 37 | 277 |
Oct | 29 | 54 | 68 | 525 |
Nov | 20 | 87 | 82 | 577 |
Dec | 17 | 35 | 61 | 401 |
Jan | 21 | 36 | 64 | 620 |
Feb | 26 | 58 | 80 | 652 |
Mar | 24 | 75 | 70 | 495 |
Apr | 21 | 70 | 74 | 514 |
this bit of AWK
code
#+begin_src awk :stdin inventory-shipped :exports results $1 ~ /J/ #+end_src
returns this subset of the table
Jan | 13 | 25 | 15 | 115 |
Jun | 31 | 42 | 75 | 492 |
Jul | 24 | 34 | 67 | 436 |
Jan | 21 | 36 | 64 | 620 |