Skip to content
Advertisement

how to rearrange all paragraphs from vertical to horizontal (all next to each other)

I have a file with about 25 paragraphs, they are separated by a blank line, each paragraph has about 2 to 20 lines, and each line has 5 columns separated by a semicolon.How can I move paragraph 2 next right to paragraph 1,paragraph 3 next right to paragraph 2 and so on. My goal is to paste everything later into Excel. Numbers used here are just example for any words/numbers.

111;111;111;111;111;
111;111;111;111;111;

222;222;222;222;222;
222;222;222;222;222;
222;222;222;222;222;

333;333;333;333;333;
333;333;333;333;333;
333;333;333;333;333;
333;333;333;333;333;

desired output:

111;111;111;111;111; 222;222;222;222;222; 333;333;333;333;333;    
111;111;111;111;111; 222;222;222;222;222; 333;333;333;333;333;    
                     222;222;222;222;222; 333;333;333;333;333;    
                                          333;333;333;333;333;

Advertisement

Answer

Have a look at my answer to How to move everything following a dash to a new column?.

If your solution does not require to be based on awk, the following code might do what you want:

csplit -f tempfile in.txt '/^s*$/+1' {*}; paste tempfile* > out.txt

The output for your sample data is

111;111;111;111;111;    222;222;222;222;222;    333;333;333;333;333;
111;111;111;111;111;    222;222;222;222;222;    333;333;333;333;333;
    222;222;222;222;222;    333;333;333;333;333;
        333;333;333;333;333;

As you can see, tabs separate columns, which is a good idea if you want to do some post-processing on out.txt.

If an awk-solution is required, you obtain the same result as above with:

awk 'BEGIN{FS="n"; RS="nn";}
     {for(i=1; i<= NF;i++)
        ar[NR,i]=$i
      nf = nf <NF? NF : nf}
     END{  for(j=1; j <= nf; j++){
             str=""
             for(i = 1; i <= NR; i++){
                  str = str""ar[i,j]"t"
             }
             print str
           }
         }' in.txt

However, if you want some nice alignment for the human eye, you will have to add some additional code (i.e., determine the longest field in each column, fill all other entries, …) to get what you want.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement