banner

For a list of BASHing data 2 blog posts see the index page.    RSS


AWK one-liners to multi-liners

GNU AWK offers a lot of flexibility in writing a command, and if you can see the command in the shell you can make quick adjustments and edits before executing.

For that reason I usually write my commands as one-liners like the one below, in which I'm adding up the numerical entries in field 3 of a CSV:

demo:
 
fld1,fld2,fld3
aaa,XXX,20
bbb,XXX,10
ccc,YYY,5
 
awk -F"," '{sum+=$3} END {print sum}' demo

Notice that I didn't have to tell AWK to ignore the first (header) line when doing my arithmetic operation. Input that looks numeric is numeric. All other input is treated as strings, says the GNU AWK manual.

Sometimes my one-liners get pretty long, and it can be hard to see just what AWK is being told to do. If that's the case, GNU AWK has a very handy option, -o[file], that pretty-prints the command as a multi-liner. Note that there's no space between -o and [file], and to see the pretty-print in the shell you can replace [file] with "-". Importantly, the pretty-printed command does not execute. Pretty-printing lets you inspect a multi-line version of your one-liner without launching it.

awk1

Although the pretty-print doesn't include AWK options, the options are still visible in the -o- command. The long and fairly confusing one-liner below is taken from my fldpair function:

awk2

Last update: 2024-05-10
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License