awk nawk gawk

The awk Command

   

Introduction

awk is a pattern matching file processor. The simplest way to visualize what awk does is to think of a large colon-delimited text file. awk will process each line of the file, split the content up into fields, and then perform operations on those fields.

While most of the Unix commands we write guides for have a one track mind, awk really doesn't have a standard 'format' or a use that is vastly more common than another. awk can be used like 'cut', to isolate fields in a file, or it can act as a fully functional, programmatic text processor. We'll give examples for both.
For now, we'll stick with a simple introduction.

awk Format:

There are two formats for awk, one allows you to write the AWK code right in the command line, the other allows you to use an AWK program file.

AWK code in command line:
awk <program> <input file>

Where a 'program' is formatted like this:

/pattern/ {'action'}
AWK code contained in local file:
awk -f <awkcode.awk> &ly;inputfile>
				

Quick Links!

A Programmer's Introduction to AWK
awk's List of Operators
awk, nawk, gawk - What's the difference?
A simple awk example
Use awk to reformat the default unix date command

A Programmer's Intro to awk

awk, nawk, gawk - What's the difference?

In short:
As of recently, 'awk' should work just fine on most Linux / Unix systems. If your distribution contains awk, nawk, gawk, etc. There's typically a symlink from 'awk' to whichever incarnation your distribution uses.
Linux distributions can also rely on 'gawk' being present.
Unix systems typically just use 'awk'.
Debian and Ubuntu users can reliably use 'mawk', which is a very fast implementation based on a bytecode interpreter.

Additional Information:

Simple, yet thorough, example

This is the simplest awk example that I can think of that both, does justice to what the language actually does, and also doesn't completely confuse most folks. This example scans through each line of an input file, prints the second field, a dash, the number of fields on that line, and the current line number. Enjoy.
awk {$2 " - " NF " " NR} <input.log>

Output:

 - 1 1
System - 6 2
system: - 3 3
 - 1 4
Checking - 7 5
 - 1 6
System - 6 7
system: - 3 8
 - 1 9
Checking - 7 10
Remember that we're printing the second field on each line ($2), the Number of Fields (NF), and the Number of Records (or rows if you prefer) (NR)
The rows of output without any data before the dash, simply didn't have anything in the second field for those records.

Common Uses

Reformat the date using awk

While the default unix date is handy, it's a bit verbose for many uses:
Mon Sep 3 13:42:23 EDT 2007

Just a few seconds with awk can