If you want the headers to line up with the columns, you can use the aptly named column
utility (a BSD extension, but it also comes with most Linux distros). To reformat existing text into aligned columns, use the -t
option.
You can insert the column headers using a compound statement:
command1 | command2 | { echo Header1 Header2 Header3; command3 } | column -t
or:
{ echo Header1 Header2 Header3; command1 | command2 | command3 } | column -t
(whichever you find more readable.)
Note that the headers may not have spaces in them, and nor may the data-elements. If your data is not white-space separated, you can specify a different delimiter with the -s
option; remember to use the same delimiter for your headers.
column
left-justifies all columns, so numeric columns don't look as nice as you might want them to.