funkyfrogstock - Fotolia


Get to know the awk command for Linux

Awk lets you find and process patterns in text. It can get complicated, but the basics are pretty simple and a good place to start learning about Linux commands.

Awk is a very powerful Linux command-line tool. Programs can get quite complex, but it's important to start with...

the basics.

Awk is an interpreted programming language developed for text processing by Alfred Aho, Peter Weinberger, and Brian Kernighan in 1977. The initials of the three authors' last names make up the program's name. Various forms of awk (nawk, gawk and others) are available in almost every version of Linux, and they are easy to use from the command line. Being a powerful command-line tool also means awk is effective over secure shell.

The language is used to process text files. By default each line of a file is treated as a record. The record is then further broken up into a sequence of fields. Awk programs are just a list of pattern-action statements that are performed on the records and fields. Awk reads through the files sequentially and when it matches a pattern, it prompts a related action.

Pattern matching with the awk command

For example, say I want to print out all the lines in a file that match a certain string.

First I need a text file. You could use the ls command with a couple of options to get a listing of your local directory to use as a file. The following command line runs ls with the -l (long) and -h (print kilobyte or megabyte files sizes) and redirects the output to a text file named ob-list.txt.

ls -lh > rob-list.txt

Here's what the resulting rob-list.txt looks like.

total 40K

-rw-rw-r-- 1 rob rob 23K Jul 12 15:29 awk-basics.odt

-rw-rw-r-- 1 rob rob 110 Jul  7 12:52

-rw-rw-r-- 1 rob rob 220 Jul 12 16:26

-rwxrwxrwx 1 rob rob  59 Jul 12 16:28 rob.awk

-rw-rw-r-- 1 rob rob 220 Jun 27 10:55

-rw-rw-r-- 1 rob rob   0 Jul 12 16:57 rob-list.txt

An awk command line to find "220" might look like this.

awk '/220/ {print $0}' rob-list.txt

And here's the result.

-rw-rw-r-- 1 rob rob 220 Jul 12 16:26

-rw-rw-r-- 1 rob rob 220 Jun 27 10:55

In this case, the text between the slashes is matched as awk moves from the beginning to the end of the file. The $0 field represents the entire line, and you can print only certain fields from each line.

If you want to print only the file size and file name of each line in the file, you would do something like this: The file size in the text file is field number 5. The file name is field 9. White space is the default field separator. This awk command line shows only the file size and file name:

awk '{print $5 " " $9}' rob-list.txt

And this is the result:

23K awk-basics.odt



59 rob.awk


0 rob-list.txt

If you looked at the text on the actual command line on your computer screen, there'd be a blank line at the beginning of the print out. It's there because awk just prints fields 5 and 9, even if it's a blank line.

Possible combinations

You could then combine matching with printing certain fields. For example, if you just want find the lines that contain the string "220" and then print out the file size and name, this line will do the trick:

awk '/220/ {print $5 " " $9}' rob-list.txt

The output looks like the following:



Be aware that when you use the matching option '/ target string /' the awk command matches all occurrences of the string in each line of the text file. It's conceivable that you might have a 220 on the line somewhere other than in one of your printed fields. The matched line still shows up, even though in this case the file size (field 5) may not be 220.

Obviously, these examples use very small text files. Awk can handle much larger files, with tens or even hundreds of thousands of lines of text. Awk simply starts at the beginning and methodically steps through the file until it reaches the end, matching patterns, printing and outputting text as it goes.

You can also get a bit more advanced and look for patterns in the fields themselves. For example, maybe you want to search with some conditions and print out just those lines:

awk '(index($9, "rob") != 0) && (index($9, "awk") !=0) {print $5" "$9}' rob-list.txt

The result would be:

59 rob.awk

Here the index function finds instances of "rob" and "awk" in the rob-list.txt. In this case I used the && (and) operator to just print out the line(s) with both. You could also use || operator to do an "or" comparison.

You could also substitute in data when you find certain strings. With a simple change to the last awk command, you could substitute "I found it" into the output when a line with "rob" and "awk" are found together. For example:

awk '(index($9, "rob") != 0) && (index($9, "awk") !=0) {print $5" I found it"}' rob-list.txt

This is the output:

59 I found it

Putting awk to work

I've used awk in some Internet of Things projects to move data between platforms and applications. For example, I have taken the data from a DS18B20 digital temperature sensor, run it through Arduino and output the text data via XBee radios to a Linux notebook. The data was simply pulled into the notebook using the cat command to query the USB port, then redirect the data to a text file.

cat /dev/ttyUSB0 >

Here's some of the data right out of the Arduino:











This data represents temperature readings from two different sensors, "a001" and "a002," that are polled about once per second. I used awk to separate the readings and put them in a form that could be read into the kst graphing program on a Linux notebook.

I used the following awk command line to prep the data for the kst graphing program.

awk -F "|" '{print $3","$4}' < >

The data output is now just X and Y coordinates, separated by a comma, that feeds into kst.











Notice that you can subtly change the format of the output simply by adding characters, spaces and so on. Awk is ideal for data format conversion jobs.

Next Steps

There's a vast treasure trove of knowledge and information on the Web about awk, these are just a few basic examples. Check out the gawk user's guide, the awk man page and an awk tutorial. You can even get a version of awk for Windows. The Web is also full of sites dedicated to awk one-liners. Try Another good one is has a nice list of one-liners dealing with line spacing and numbering.

Dig Deeper on Alternative operating systems