Retrieve Column Based On Column Name In AWK

This is a quick snippet to show how to grab a column based on its name (rather than number) in AWK. This can be useful when you are unsure if the output you are processing may one day change. This makes for a slightly more flexible / resilient script.

Lets say we have some columnar output, in this case from df -h, that looks like this:

It is unlikely that df -h will change its output, but many of the tools you might use on a regular basis may be less stable.

Lets say we want to grab the ‘Size’ column.

Rather than using df -h | awk 'NR>1 { print $2 } we can instead match the column named ‘Size’ with the following:

df -h | awk 'NR==1 { for (i=1;i<=NF;i++) if ($i ~ /Size/) COLUMN_NUM=i } NR>1 { print $COLUMN_NUM }'
Code language: HTML, XML (xml)

This starts by checking which row we are on (NR==1). If we are on the first row (the header row) we loop through each field until one matches “Size”. Then we record the number of this field in the COLUMN_NUM variable. This variable is now available through the subsequent AWK actions. On subsequent rows we use to print only the Size column.

Retrieve Column Based On Column Name In AWK

Leave a Reply

Your email address will not be published.