From charlesreid1

Revision as of 22:06, 11 February 2011 by Admin (talk | contribs) (Created page with "Awk is a really powerful data-driven programming language. = Basics = The basic syntax of an awk program is: <syntaxhighlight lang="bash"> awk 'condition { statement }' </synt...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Awk is a really powerful data-driven programming language.

Basics

The basic syntax of an awk program is:

awk 'condition { statement }'


Parsing

You can parse a list using awk by using the -F flag, followed by the delimiter (by default, it is space). Each token can then be referred to using $1, $2, $3, etc.

$ echo "token1.token2.token3"
token1.token2.token3

$ echo "token1.token2.token3" | awk -F. '{print $1}'
token1

$ echo "token1.token2.token3" | awk -F. '{print $2}'
token2

This can be done with any character, not just punctuation:

$ echo "token1atoken2atoken3"
token1atoken2atoken3

$ echo "token1atoken2atoken3" | awk -Fa '{print $1}'
token1

$ echo "token1atoken2atoken3" | awk -Fa '{print $2}'
token2