Shell – simple command for outputting tab-delimited columns

catcolumnscommand lineshell

E.g. I have a file (produced with echo -e "var1\tvar2\t\var3\tvar4" > foo) that are output as:

$ cat foo
case    elems   meshing nlsys
uniform 2350    0.076662        2.78
non-conformal   348     0.013332        0.55
scale   318     0.013333        0.44
smarter 504     0.016666        0.64
submodel        360     .009999 0.40
unstruct-quad   640     0.019999        0.80
unstruct-tri    1484    0.01    0.88

I'd prefer the output like this (here I used vim and :set tabstop=14):

case          elems         meshing       nlsys
uniform       2350          0.076662      2.78
non-conformal 348           0.013332      0.55
scale         318           0.013333      0.44
smarter       504           0.016666      0.64
submodel      360           .009999       0.40
unstruct-quad 640           0.019999      0.80
unstruct-tri  1484          0.01          0.88

I can get the same functionality with cat if I use $ tabs=15 in bash (see this question). Is there a program that does this kind of formatting automatically? I don't want to experiment with the tabs value before cating a file.

Best Answer

I usually use the column program for this, it's in a package called bsdmainutils on Debian:

column -t foo

Output:

case           elems  meshing   nlsys
uniform        2350   0.076662  2.78
non-conformal  348    0.013332  0.55
scale          318    0.013333  0.44
smarter        504    0.016666  0.64
submodel       360    .009999   0.40
unstruct-quad  640    0.019999  0.80
unstruct-tri   1484   0.01      0.88

Excerpt from column(1) on my system:

...

-t      Determine the number of columns the input contains and create a
        table.  Columns are delimited with whitespace, by default, or
        with the characters supplied using the -s option.  Useful for
        pretty-printing displays.

...