Aligning qhost output on the commandline when hostnames are too damn long

qhost is a UNIX command line tool to print the status of nodes on a Grid Engine system.

The output is normally quite readable and is sorted by columns to give information on the hostname (“HOSTNAME”), architecture (“ARCH”), no. of CPUs (“NCPU”), processor load (“LOAD”), total available memory (“MEMTOT”), current memory usage (“MEMUSE”), swap memory size (“SWAPTO”) and current swap usage (“SWAPUS”) of each node on the cluster.

Unfortunately, when the hostnames are too long, instead of truncating them to keep the columns aligned the row gets shunted along, making the output messy and much harder to read quickly. Example ( in this case you probably also get wrapping because the main frame of this WP template is too narrow, sorry!):

global                  -               -     -       -       -       -       - lx26-amd64      8  2.99   15.6G    7.0G    7.5G   74.1M lx26-amd64      8  1.05   11.7G    2.7G    7.5G     0.0 lx26-amd64     12  0.01    7.8G  229.9M   16.0G     0.0 lx26-amd64      4  0.51   15.5G  847.8M    7.9G   48.4M lx26-amd64      8  2.88   11.7G    5.0G    7.5G  158.6M lx26-amd64      8  1.99   23.5G    2.8G    7.5G     0.0
localhost               lx26-amd64      4     -    7.7G       -    7.9G       - lx26-amd64      8  3.57   11.7G    4.1G    7.5G  324.0K lx26-amd64     32 20.40  125.9G   18.4G   16.0G     0.0 lx26-amd64     32 24.80  125.9G   21.6G   16.0G     0.0

I am going to pipe the output to awk and see if I can fix it by trimming the first column while leaving the rest as they are.

First, let’s start by reviewing how to print certain columns only, using awk, To print just columns 2 and 3 from the qhost output we use.

$ qhost | awk '{print $2, $3}'

The comma makes sure that awk puts a space between the columns.

But using printf gives us more formatting options. The following command gives output with all columns aligned. The formatting part is in the “quotation marks” and the arguments given to it follow the commas. We can truncate the first column string (“s”) to 8 characters and specify tabs (“\t”) between each column and a newline (“\n”) at the end of each row:

qhost | awk '{printf("%.8s \t%s \t%s \t%s \t%s \t%s \t%s\n", $1, $2, $3, $4, $5, $6, $7)}'

And actually I don’t need the architecture column, so the following gave me the best results:

qhost | awk '{printf("%.8s \t%s \t%s \t%s \t%s \t%s\n", $1, $3, $4, $5, $6, $7)}'

There are, surely, more advanced and perhaps more elegant solutions but this is a simple way to get the job done to a satisfactory degree.