Posted: . At: 3:42 PM. This was 2 years ago. Post ID: 15680
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to add a column heading with awk on Linux.


This very nice one-liner will print some information about your mounted drives on Linux and also print a column header to dress up the output.

┌──[jason@11000000.10101000.00000001.00000011][~/Downloads]
└──╼  ╼ $ df -Hla | grep "[0-9]% /" | awk -F, 'NR==1 {print "Device","Capacity","Free","Usage","Percentage","Mount"} {gsub(/"/,""); print $1,$2,$4}' | column -t
Device     Capacity  Free  Usage  Percentage  Mount
dev        13G       0     13G    0%          /dev
run        13G       1.2M  13G    1%          /run
/dev/sda3  34G       6.3G  26G    20%         /
tmpfs      13G       230k  13G    1%          /dev/shm
tmpfs      13G       5.0M  13G    1%          /tmp
/dev/sdb1  984G      758G  177G   82%         /mnt/ubuntu
/dev/sda6  838M      115M  723M   14%         /boot
tmpfs      2.6G      8.2k  2.6G   1%          /run/user/1001

This is the awk statement to add a nice header to the output, this looks very good.

awk -F, ‘NR==1 {print “Device”,”Capacity”,”Free”,”Usage”,”Percentage”,”Mount”} {gsub(/”/,””); print $1,$2,$4}’ | column -t

This adds a nice touch to the output.

print $1,$2,$4}'

Edit the section above to add or remove needed columns. But this does work very well. Very useful in a script that returns useful information.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.