I'd like to output a table format text. What I tried to do was echo the elements of an array with '\t' but it was misaligned. my code
for((i=0;i<array_size;i++));
do
echo stringarray[$i] $'\t' numberarray[$i] $'\t' anotherfieldarray[$i]
done;
My output
a very long string.......... 112232432 anotherfield
a smaller string 123124343 anotherfield
Desired output
a very long string.......... 112232432 anotherfield
a smaller string 123124343 anotherfield
awk
solution that deals with stdinSince
column
is not POSIX, maybe this is:Test:
Test commands:
Output for all:
See also:
It's easier than you wonder.
If you are working with a separated by semicolon file and header too:
If you are working with array (using tab as separator):
Use column command:
Not sure where you were running this, but the code you posted would not produce the output you gave, at least not in the bash that I'm familiar with.
Try this instead:
Note that I'm using the group seperator character (1d) intead of tab, because if you are getting these arrays from a file, they might contain tabs.
printf
is great, but people forget about it.Notice I used
%10s
for strings.%s
is the important part. It tells it to use a string. The10
in the middle says how many columns it is to be.%d
is for numerics (digits).man 1 printf
for more info.SAMPLE RUNS
REF LIB at: https://github.com/gdbtek/linux-cookbooks/blob/master/libraries/util.bash