Fernando Brito Lopes
Fernando Brito Lopes

Reputation: 101

Format two columns width using printf

I have a file with two columns like this:

73697695 1111111100110100211010101100000110100111  
73697715 0100001010100022000020000000000200200000  
73698148  0000000000200000000200220001100100010111  
73698210 1111111211011012001011000001111000001110  
73698229 1111111100110111110020101000000111210011  
736985237658373533  0000000110100011101010000001100100100000  
73698858 1111111210010011101010000001100100100111  
73698887 2222222200020000202000000010001100110000  
73699163 2222222200020100110110211100000100100100  
7369929986423  2222222200020100110110211100000011110111  

I am trying to create a bash file to format its columns. First I need to find the maximum length of the first column. So, I did this:

lengthID=$(awk '{ if (length($1) > max) max = length($1) } END { print max }' file)

After that I would like to apply that "lengthID" to such file to align the first column to the right based on the "lengthID". The second column must be align to the left. I tried use the following command line, but it did not work.

awk '{printf("%"lengthID"s%1s%" "s\n",$1," ",$2)}' file > temp && mv temp file

I know that the maximum length is 18 (lengthID = 18). So, the following command would work:

awk '{printf("%18s%1s%" "s\n",$1," ",$2)}' file > temp && mv temp file

And I would get a file like this (exactly what I need). However, I'd like to automatically find such length and use it.

          73697695 1111111100110100211010101100000110100111
          73697715 0100001010100022000020000000000200200000
          73698148 0000000000200000000200220001100100010111
          73698210 1111111211011012001011000001111000001110
          73698229 1111111100110111110020101000000111210011
736985230909090909 0000000110100011101010000001100100100000
          73698858 1111111210010011101010000001100100100111
          73698887 2222222200020000202000000010001100110000
          73699163 2222222200020100110110211100000100100100
     7369929986423 2222222200020100110110211100000011110111

Does anyone know a way to do it?

Thank you.

Upvotes: 0

Views: 749

Answers (1)

Ed Morton
Ed Morton

Reputation: 203324

$ awk 'NR==FNR{c=length($1); w=(w>c?w:c); next} {printf "%*s %s\n", w, $1, $2}' file file
          73697695 1111111100110100211010101100000110100111
          73697715 0100001010100022000020000000000200200000
          73698148 0000000000200000000200220001100100010111
          73698210 1111111211011012001011000001111000001110
          73698229 1111111100110111110020101000000111210011
736985237658373533 0000000110100011101010000001100100100000
          73698858 1111111210010011101010000001100100100111
          73698887 2222222200020000202000000010001100110000
          73699163 2222222200020100110110211100000100100100
     7369929986423 2222222200020100110110211100000011110111

You sure you can't just use column -t file though?

$ column -t file
73697695            1111111100110100211010101100000110100111
73697715            0100001010100022000020000000000200200000
73698148            0000000000200000000200220001100100010111
73698210            1111111211011012001011000001111000001110
73698229            1111111100110111110020101000000111210011
736985237658373533  0000000110100011101010000001100100100000
73698858            1111111210010011101010000001100100100111
73698887            2222222200020000202000000010001100110000
73699163            2222222200020100110110211100000100100100
7369929986423       2222222200020100110110211100000011110111

The alignments different but if all you need are visual columns maybe that's OK.

Upvotes: 1

Related Questions