I have data in a csv file. I wrote a script that cat
s this file and uses column -s, -t
to nicely tabularize it into nice columns:
Foosballs Barbells Bazketballs
22 39 14
86 94 37
17 44 28
However, I'd like to display the header row in bold. I can do that by writing the format codes directly to the file.
bold=$(tput bold)
reset=$(tput sgr0)
echo "${bold}Foosballs,Barbells,Bazketballs${reset}" > /path/to/file
This works fine with cat
; the format codes are displayed correctly when I cat the file. But they screw up column -t
: any colored/bolded row is no longer aligned with the other rows.
Foosballs Barbells Bazketballs
22 39 14
86 94 37
17 44 28
Is there some way to get column -t
to ignore color codes when lining up data into columns? (Or is there a better way to display csv data in columns?)
UPDATE:
Applying column
first and the format codes second will work, as some answers point out. But in many cases I want to apply different formats/colors to individual values in the row, not to the entire row. Here's a simple example:
echo "${underline}foo${reset} ${underline}bar${reset}"
In general, I might want to use arbitrary formatting logic that's difficult or impossible to apply post-hoc (i.e., after I've already printed the line and called column -t
on it). Formatting after tabularizing (as in Charles Duffy's answer) is a great start but may not always work for me (at least, conveniently).
I could always write a utility to do this format-code-transparent tabularization myself, but then I'd have to bring that with me wherever I work. I don't want to have to know column widths in advance; I need something like column -t
I can throw on the end of a pipe with arbitrary delimited data. Basically, I need a clever one-liner or a third-party util that's readily available via Homebrew or other package managers.
To sum up: For the bounty, I'm looking for a simple, (reasonably) portable method to tabularize previously-formatted data.
column
, right?column
fromutil-linux-2.36
but I can confirm thatcolumn
used on FreeBSD (which is probably more similar to what you have on Mac) botches the output.