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

Fixing Apache 2.4 – No Errors In Log – Connection Refused

I recently upgraded my Macbook to OS X El Capitan (10.11). As expected, Apache did not immediately work upon upgrade. In this case, it was because upgrading from Yosemite to El Capitan also upgraded Apache from 2.2 to 2.4.

Unfortunately, unlike in times past, this time Apache did not give me any feedback as to why it was not working. When I ran curl localhost from the command line I simply got:

curl: (7) Failed to connect to localhost port 80: Connection refused

While my web browser gave a standard “This site can’t be reached” error.

Not helpful. I found nothing in the Apache error log (/var/log/apache2/error_log) either.

I tried restarting apache (sudo apachectl restart). Still nothing.

However, this little command helped me tremendously:

apachectl configtest

This revealed a syntax error in my apache configs. In my case, I was attempting to use the SSL module (specifically, calling SSLEngine in my passenger config) without actually loading it. If you aren’t seeing anything in the apache logs, despite the fact that running sudo apachectl start returns a message saying apache is already running, then you may also have a syntax error somewhere in your configs. Try out apachectl configtest and see what you find.

In my situation, uncommenting “LoadModule ssl_module libexec/apache2/” in httpd.conf did the trick and I got apache up and running.

Hopefully this will help a fellow developer whose sysadmin skills, like mine, could use some work.

Fixing Apache 2.4 – No Errors In Log – Connection Refused