en en en en en en en en en en en en en en hi en en kk en ky en en kk en ky en en en en en en en en en en
Alain Kelder | Output data in columnar format, automatically adjusting column width based on longest record
Alain Kelder is a Giant Dork
28 Jun 2012 Debian, Linux Trackback
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:
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:
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:
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
Mail (will not be published) (required)
NOTE: Enclose quotes in   . Enclose code in    (where LANG is one of these).
Alain Kelder's blog is member of the GiantDorks.org network. Join us and embrace the Giant Dork in you (while saving the world in the process)!
Posts | Comments | Tides and Winds around Disco Bay
Content by Alain Kelder is licensed under a Creative Commons Attribution 3.0 United States License.
If you're a spam bot, please send your important spam here.