2

Let's say I have some CLI one-liner, which outputs some lines of text with space-separated parts. Those parts should logically be columns, but because of text width it doesn't look so.

How could I automatically format such output to make it pretty columns?

For example, I have output like

Alice param1 param2345 32768 50 16
Bob param2345 param1 512 10 1
_debug_user_ param0 param0 0 0 0

And I want to make it like

Alice        param1    param2345 32768 50 16
Bob          param2345 param1    512   10 1
_debug_user_ param0    param0    0     0  0
6

With Linux column(1):

column -t <file.txt

With BSD rs(1):

rs 0 6 <file.txt

With awk(1):

awk 'FNR==NR { for(i=1; i<NF; i++) if(length($i)>w[i]) w[i]=length($i) }
     FNR!=NR { for(i=1; i<NF; i++) $i=sprintf("%-" (w[i]+1) "s", $i); print }' \
        file.txt file.txt
  • column -t is what I searched for, thank you! – AntonioK Oct 11 '16 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.