zara zara - 4 months ago 8
Linux Question

How to put different separators between columns in a file in linux?

I have a huge data file in which there is space between columns as a separator.
I wan to put tab between every other 2 coloumn and the need to be 2 space between each pair of columns. As a small example to clarify what I mean:

input file:

1 1 1 1 2 1 2 2 2 1 1 1 2 2 1 2

1 1 1 1 1 1 2 2 1 1 1 1 2 2 2 2

1 1 1 1 1 1 2 2 1 1 1 1 2 2 2 2


I want the output file be like:

1 1 1 1 2 1 2 2 2 1 1 1 2 2 1 2
1 1 1 1 1 1 2 2 1 1 1 1 2 2 2 2
1 1 1 1 1 1 2 2 1 1 1 1 2 2 2 2


Any suggestion please? Note that the real file has more than 50,000 columns and rows.

Answer
$ awk '{for (i=2;i<=NF;i+=2) printf "%s  %s%s", $(i-1), $i, (i<NF ? "\t" : ORS)}' file
1  1    1  1    2  1    2  2    2  1    1  1    2  2    1  2
1  1    1  1    1  1    2  2    1  1    1  1    2  2    2  2
1  1    1  1    1  1    2  2    1  1    1  1    2  2    2  2