Table Formatting

Many text files contain tabular data. Unfortunately, getting the columns to line up can prove difficult. For example, here is a typical file:

/dev/cdrom /mnt/cdrom iso9660 user,unhide,ro 0 0 
/dev/scd1 /mnt/cdrom1 iso9660 noauto,unhide,user,ro 0 0 
/dev/scd0 /mnt/cdrom2 iso9660 noauto,unhide,user,ro 0 0 
/dev/loop0 /mnt/cdrom_l iso9660 noauto,unhide,user,ro 0 0 
/dev/sda1 /mnt/optical ext2 defaults,noauto,user 0 0

It would be nice to see something like this:

/dev/cdrom /mnt/cdrom   iso9660 user,unhide,ro        0 0 
/dev/scd1  /mnt/cdrom1  iso9660 noauto,unhide,user,ro 0 0 
/dev/scd0  /mnt/cdrom2  iso9660 noauto,unhide,user,ro 0 0 
/dev/loop0 /mnt/cdrom_l iso9660 noauto,unhide,user,ro 0 0 
/dev/sda1  /mnt/optical ext2    defaults,noauto,user  0 0

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.