17 February 2010

format columns in awk with printf

I was struggling with awk to display the diskspace on my Linux host in the most convenient way. One problem was that a df command splits the output on two lines, but this is fixed by df -P.

Formatting the output with awk required using printf instead of print. With printf you can give formatting to the columns. I came on the following which is seems very usefull in my case.

oracle@myserver:~> df -Ph | awk '{ printf "%-12s%8s\n", $6 , $5 }'

Mounted Use%

/ 9%

/dev 1%

/boot 10%

/home 4%

/opt 31%

/tmp 20%

/usr 52%

/var 11%

/data 15%

/backup 83%