Sound advice - blog

Tales from the homeworld

My current feeds

Sun, 2005-Feb-06

Awk one-liners

I felt I had to respond to this post by Bradley Marshall on perl one-liners. My position as an awk advoate is a long-suffering one and one that could do with some updating :)

Given the following data set in a file:

foo:bar:baz

The following one-liner will pull it out in a useful fashion:

$ awk -F: '{print $2}' filea
bar

A neat extension is, given a dataset like:

1:2:3:4
5:6:7:8

You can use a one liner like the following:

$ awk -F: '{tot += $1};END{print tot}' fileb
6

I, for one, feel cleansed. Now wasn't that a touch easier and more legible? :) As always, see the awk manpage for more information...

Benjamin