Unix How-To: awk is still a very handy tool

By Sandra Henry-Stocker  Add a new comment

Awk does a lot more than select a column from a file or an input stream. It can select columns from selected rows. It can calculate totals, extract substrings, reverse the order of fields and provide a whole lot of other very handy manipulations. Whether you squeeze your awk permutations onto the command line or prefer to build them into scripts, the language is clever and versatile and well worth using even as it joins the ranks of middle-aged computer utilities.

If you want to use awk to add up a bunch of numbers formulated as a column in a text file, you can use a one-liner like this:

$ awk '{ SUM+=$1 } END { print SUM }' < nums

That SUM+= operation adds the contents of column one to a running total for each line of input. The sum is only printed at the very end when input is exhausted. You can change $1 to the column of your choice or $NF if you want to add up the rightmost column.

If you prefer scripts to the command line, you could use a script like the addcol script below to sum whatever column you choose. You would just pass the column you want to add on the command line as the COL parameter:

$ awk -f addcol COL=3 < numbers

The -f tells awk to run the designated script (addcol) and COL=3 passes "3" as the column number you want to sum.

# addcol
BEGIN { SUM=0 }
{
  print $COL
  SUM += $COL
}
END { print "Sum: " SUM }

Awk has some grep-like features as well. If you want to operate only on lines that contain some particular text, you can specify that text on the command line like this:

$ awk '/choose me/' textfile

You can also combine search options using && (and), || (or), and even negation operators. The command below selects lines that contain both the word "this" and the word "that". Using '/this/ && !/that/' would select only lines that contain "this" without containing "that".

$ awk '/this/ && /that/' notes
# find the process that started this one
# make sure that this user provided an answer

You can also select content based on its position within your input. In the command below, we only want to see lines eleven and above.

$ awk 'NR > 10' counts
11 63
12 99
13 63
14 77
15 41

And, of course, there are a lot of other nice little tricks available.

Awk provides some quick and effective filtering and incorporates an easy syntax. Probably the only thing that takes some getting used to is referring to parameters without putting dolar signs in front of them.

ITworld LIVE

Operating SystemsWhite Papers & Webcasts

White Paper

A Comparison of PowerVM and VMware vSphere (4.1 & 5.0) Virtualization Performance

This technical white paper presents benchmark results showing greater VM consolidation ratios than demonstrated in previous benchmarks and demonstrating the extent of the performance lead that PowerVM virtualization technologies deliver over x86-based add-on virtualization products.

White Paper

Consolidating Lotus Domino x86 Workloads on IBM Power Systems

Read the white paper to learn how moving up to Lotus Domino 8.5 and consolidating with IBM Power Servers can help you boost performance results and ROI.

White Paper

Task, workflow & issue management for teams. Try free!

Need a flexible system for managing team tasks, issue tracking, and automating and managing workflow processes? Comindware® Tracker helps you do it all.

Webcast On Demand

Best Practices in Monitoring VMware

The benefits of virtualization are unassailable: increased agility, scale, and cost savings to name a few. However, so too are the monitoring challenges posed by these environments-including complexities, lack of visibility and control, and inefficiency.

Sponsor: Nimsoft

White Paper

How Nimsoft Service Desk Speeds Deployment and Time to Value

For years, many support teams have been hamstrung by their traditional service desk platforms, which require complex, time-consuming coding for virtually every aspect of customization. This complexity makes it costly and difficult for support organizations to adapt-and places an increasingly substantial burden on the agility and efficiency of the business as a whole.

See more White Papers | Webcasts

Ask a question

Ask a Question