Home > Back-end >  Moving text, side by side after n number of lines
Moving text, side by side after n number of lines

Time:09-09

I am trying to move text side-by-side after a gap in the text.

My example is

684 0.00000 \
685 0.00000 \
686 0.99490 

684 0.00000 \
685 0.00000 \
686 0.76000

I would like the text to be as follows

684 0.00000 684 0.00000 \
685 0.00000 685 0.00000 \
686 0.99490 686 0.76000

I have tried but this hasn't produced the desired output.

awk 'NR%1{printf "%s ",$0;next;}'1 file.txt 

Any help or assistance would be greatly appreciated.

Dan

CodePudding user response:

$ pr -2t file

there will be an empty line at the end, which can be trimmed by piping to ... | sed '$d'

CodePudding user response:

One way is to set a column when seeing the empty line, using multidimensional arrays. Something like this:

BEGIN{
 column=0
 i=1
}
{
if ($0=="") {
    column=1
    i=1      # reset again
    next     # and move on
  }
  arr[column, i]=$0
  i =1
}

END{
    for (myindex = 1; myindex < i;   myindex) {
        print arr[0,myindex] "  "  arr[1,myindex]
    }
}

Given data:

a
b
c

d
e
f

result will be

a b
c d
e f

NOTE: I did not worry about the backslashes. You did not explain their importance.

  • Related