Search code examples
pythonawkcontrol-m

How to wrap one column of data(text) into 8 columns and can be dropped in excel with 8 columns style?


I have a long column of text data, which likes this:

apple
162
30.45%
newyork
red
2018-12-10  22:48
3.23
Nop12345
pear
20
14.56%
washington
green
2018-12-09  10:30
4.24
Nok45367

I want it tab delimited as the following and it can be dropped in excel with 8 columns:

apple   162 30.45%  newyork red 2018-12-10 12:48    3.23    Nop12345
pear    20  14.56%  washington  green   2018-12-09  10:30   4.24    Nok45367

I have used the command

awk '{ ORS = (NR%8 ? "\t" : RS) } 1' > output.txt

to handle this stuff, the output is just like the structure that I need above if you see the results on windows accessory notepad editor, however, the truth is that it is not the 8 column style when you see it with notepad++, or some other txt editor on linux, and the worse is that it is only presented with just 2 columns if you drop it in excel as this:

apple
    162
    30.45%
    newyork
    red
    2018-12-10 12:48
    3.23
    Nop12345
pear
    20
    14.56%
    washington
    green
    2018-12-09  10:30
    4.24
    Nok45367

Solution

  • What you already have IS the right way to do what you want:

    $ awk '{ORS=(NR%8 ? "\t" : RS)}1' file
    apple   162     30.45%  newyork red     2018-12-10  22:48       3.23    Nop12345
    pear    20      14.56%  washington      green   2018-12-09  10:30       4.24   Nok45367
    

    but see Why does my tool output overwrite itself and how do I fix it? for the source of most "my output looks funny" issues.