Output data in columnar format, automatically adjusting column width based on longest record
Got a shell script that pulls data from different sources and displays output in columnar format. Initially, I used printf to line up the columns and that worked great until the script got more complicated and record length in the output started to vary.
After some searching around didn't find anything that was a good fit for the script's workflow (found some overly verbose awk and perl examples), so ended up adding a few lines of simple code to get the character count of the longest record (wc -c | sort -n | tail -n 1), doing this for each column, then constructing a printf output format string to get the data again and style using the output format string. This worked great, but added some overhead, which finally became an issue.
Salvation came in the form of the column utility, which is included by default with Debian and friends. All I had to do was pipe the output to it and it did the rest. Leveraging the column utility was about three times faster and with half the code. Hooray!
The only downside is that I lost some of the formatting flexibility that printf offers, but for this application, that was a non-issue.
A couple of simple examples of the column utility in action:
A single space delimited file:
$ cat file
Lorem ipsum dolor sit amet consectetur adipiscing
Nulla adipiscing sodales malesuada Quisque vitae mi
Phasellus accumsan ullamcorper sagittis Nulla laoreet consectetur
Vestibulum porttitor nibh vel eros mattis nec
$ column -t file
Lorem ipsum dolor sit amet consectetur adipiscing
Nulla adipiscing sodales malesuada Quisque vitae mi
Phasellus accumsan ullamcorper sagittis Nulla laoreet consectetur
Vestibulum porttitor nibh vel eros mattis nec
A tab delimited file:
$ cat file1
Lorem ipsum dolor sit amet consectetur adipiscing
Nulla adipiscing sodales malesuada Quisque vitae mi
Phasellus accumsan ullamcorper sagittis Nulla laoreet consectetur
Vestibulum porttitor nibh vel eros mattis nec
$ column -t file1
Lorem ipsum dolor sit amet consectetur adipiscing
Nulla adipiscing sodales malesuada Quisque vitae mi
Phasellus accumsan ullamcorper sagittis Nulla laoreet consectetur
Vestibulum porttitor nibh vel eros mattis nec
A comma delimited file:
$ cat file2
Lorem,ipsum,dolor,sit,amet,consectetur,adipiscing
Nulla,adipiscing,sodales,malesuada,Quisque,vitae,mi
Phasellus,accumsan,ullamcorper,sagittis,Nulla,laoreet,consectetur
Vestibulum,porttitor,nibh,vel,eros,mattis,nec
$ column -t -s, file2
Lorem ipsum dolor sit amet consectetur adipiscing
Nulla adipiscing sodales malesuada Quisque vitae mi
Phasellus accumsan ullamcorper sagittis Nulla laoreet consectetur
Vestibulum porttitor nibh vel eros mattis nec
Leave a comment