Lum – Make `column` use a single space as column separators in output

columnstext formatting

$ echo 'a b' | column -t 
a  b

There are 2 spaces between a and b in the output. How can I make column use only one space for separation when possible?

EDIT 2: Changed data to include more cases

$ cat data 
1 2 b
20 12.5 something
300 12.8 as
600 16 asrgty
1200 4.9 srty
1800 33.5 sry
10000 1.5 sty
200000 1.5 sty
$ column -t < data
1       2     b
20      12.5  something
300     12.8  as
600     16    asrgty
1200    4.9   srty
1800    33.5  sry
10000   1.5   sty
200000  1.5   sty
$  column -t -s '' < data
1 2 b
20 12.5 something
300 12.8 as
600 16 asrgty
1200 4.9 srty
1800 33.5 sry
10000 1.5 sty
200000 1.5 sty
$ column -t -s ' ' < data
1       2     b
20      12.5  something
300     12.8  as
600     16    asrgty
1200    4.9   srty
1800    33.5  sry
10000   1.5   sty
200000  1.5   sty
$ column -t < data | sed -re 's/  / /g'
1    2   b
20   12.5 something
300   12.8 as
600   16  asrgty
1200  4.9  srty
1800  33.5 sry
10000  1.5  sty
200000 1.5  sty
$ column -t < data | sed -re 's/  / /'
1      2     b
20     12.5  something
300    12.8  as
600    16    asrgty
1200   4.9   srty
1800   33.5  sry
10000  1.5   sty
200000 1.5   sty

Expected

1      2    b
20     12.5 something
300    12.8 as
600    16   asrgty
1200   4.9  srty
1800   33.5 sry
10000  1.5  sty
200000 1.5  sty

Note:
Please don't give sed hacks that work for this specific case. Questions on SE should be useful to others and not limited to highly specific data formats.

Best Answer

You can trim only the last space character in any series of space characters; like this:

sed 's/\( *\) /\1/g'

And since you always have at least two space chars in a row, this will always work to trim the splits out just as you wish:

INPUT:

column -t -s\ <<\COLS | sed 's/\( *\) /\1/g'
0 3.1415926535 s
00 3.141592653 so
000 3.14159265 som
0000 3.1415926 some
00000 3.141592 somet
000000 3.14159 someth
0000000 3.1415 somethi
00000000 3.141 somethin
000000000 3.14 something
COLS

OUTPUT:

0         3.1415926535 s
00        3.141592653  so
000       3.14159265   som
0000      3.1415926    some
00000     3.141592     somet
000000    3.14159      someth
0000000   3.1415       somethi
00000000  3.141        somethin
000000000 3.14         something

To really demonstrate how sed will honor the rule, we can append a couple copies of our columns end over end w/ each additional gaining a single space offset...

INPUT:

column -t <input | sed 's/.*/&  &   &/;s/\( *\) /\1/g'

OUTPUT:

0         3.1415926535 s 0         3.1415926535 s  0         3.1415926535 s
00        3.141592653  so 00        3.141592653  so  00        3.141592653  so
000       3.14159265   som 000       3.14159265   som  000       3.14159265   som
0000      3.1415926    some 0000      3.1415926    some  0000      3.1415926    some
00000     3.141592     somet 00000     3.141592     somet  00000     3.141592     somet
000000    3.14159      someth 000000    3.14159      someth  000000    3.14159      someth
0000000   3.1415       somethi 0000000   3.1415       somethi  0000000   3.1415       somethi
00000000  3.141        somethin 00000000  3.141        somethin  00000000  3.141        somethin
000000000 3.14         something 000000000 3.14         something  000000000 3.14         something
Related Question