AWK programming

Awk is essentially a stream editor, like sed. You can pipe text to it, and it can manipulate it on a line-by-line basis. [or it can read from a file]. It is also a programming language. That basically means it can do anything sed can do, and a lot more. (But you might have to type more :-)

Unlike sed, it has the ability to remember context, do comparisons, and most things another full programming language can do. For example, it isn't just limited to single lines. It can JOIN multiple lines, if you do things right.

The simplest form of awk is a one-liner:

  awk '{ do-something-here }'

The "do-something-here" can be a single print statement, or something much more complicated. It gets somewhat 'C'-like. Simple example:

  awk '{print $1,$3}'

will print out the first and third columns, where columns are defined as "Things between whitespace". (whitespace==tab or space)

A more complicated example:

awk '{ 	if ( $1 == "start") {
		print "started";
		if ( $2 != "" )	{
			print "Additional args:",$2,$3,$4,$5
	if ( $1 == "end") {
		print "End of section";
		printf ("Summary: %d,%d,%d (first, second, equal)\n",
			firstcol, secondcol, tied);
	if ( start >0) {
		if ( $1 > $2 ) {
			firstcol= firstcol+1
		if ( $2 > $1 ) {
			secondcol= secondcol+1

[This is just a quick showcase. How this all works, will be covered in more detail in the next lesson]

Key points to remember about variables, compared to other languages:

You can actually alter a field value. But it doesn't matter if you are using them, or setting them, you still keep the '$'
Other variables do NOT use '$'.

What does that long example do? It looks at input line by line, and wait for segments between



In-between those markers, it expects two columns of numbers. It keeps track of how many lines have the first number greater, or the second, or they are both tied. Once it hits an 'end' marker, it prints out the tally, and zeros the counters.

So, an input file of

1 3
1 5
2 1
will generate
End of section
Summary: 1,2,0 (first, second, equal)

I think that's enough for folks to digest. End of awk lesson 1.

Top of AWK lessons - Next: Chapter 2 Prev: Contents
bolthole main page