Last Updated: February 25, 2016
·
468
· allenluce

Squeeze more out of the tools

I use an old-timey command-line idiom for selecting individual words out of a pipeline:

awk '{print $2}'

I turn the $2 into $1 or $3, depending on word I want to select.

I ran into this bit of code in a script yesterday:

vagrant ssh-config | grep IdentityFile | awk '{print $2}'

I'm guessing the original author uses the idiom the same way I do.

Awk can do what grep can, and a bunch more. Pattern matching is one of the fundamental features of awk. The above line is better translated to:

vagrant ssh-config | awk '/IdentityFile/ {print $2}'

There are other cool things awk can do to make your life easier.