Is there a simple command for outputting tab-delimited columns?
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.
...
Several options:
var1=uniform var2=2350 var3=0.076662 var4=2.78
printf '%-15s %-10s %-12s %s\n' \
case elems messing nlsys \
"$var1" "$var2" "$var3" "$var4"
printf '%s\t%s\t%s\t%s\n' \
case elems messing nlsys \
"$var1" "$var2" "$var3" "$var4" |
expand -t 15,25,37
printf '%s\t%s\t%s\t%s\n' \
case elems messing nlsys \
"$var1" "$var2" "$var3" "$var4" |
column -t -s $'\t'
column is a non-standard command, some implementations/versions don't support the -s option. It computes the width of the column based on the input, but that means that it can only start displaying once all the input has been fed to it. $'...'
is ksh93 syntax also found in zsh and bash.
With zsh:
values=(
case elems messing nlsys
"$var1" "$var2" "$var3" "$var4"
)
print -arC4 -- "$values[@]"
You can also use rs
as an alternative to column -t
:
(x=$(cat);rs -c -z $(wc -l<<<"$x")<<<"$x")
-c
changes the input column separator, but -c
alone sets the input column separator to a tab. -z
sets the width of each column to the width of the longest entry of the column instead of making all columns the same width. If some lines have fewer columns than the first line, add -n
.