Friday, June 29, 2012

Tabular format output in bash using column -t

Often, commands in bash produce data that contains many similar lines that contain same fields with different values. Many times the data across these lines is not aligned because of different number of characters in values. In such scenarios column -t can help print this output in a pretty tabular way.


pankaj-> mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)


pankaj-> mount | column -t

/dev/disk0s2  on         /     (hfs,    local,    journaled)
devfs             on         /dev  (devfs,  local,    nobrowse)


As you can see piping output of mount to column with -t option prints it in a tabular format that is much more readable. By default column -t uses space as table column delimiter but it can be changed to anything else very easily:


pankaj-> cat /etc/passwd
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false


pankaj-> cat /etc/passwd | column -t -s:                                                                                                               nobody                                           *  -2  -2  Unprivileged User              /var/empty          /usr/bin/false
root                                                 *  0   0   System Administrator           /var/root           /bin/sh
daemon                                           *  1   1   System Services                /var/root           /usr/bin/false


Here the colon(:) has been used as table delimiter.


1 comment: