1

I have this file:

line1
line2
line3
line4
line5
line6
line7
line8
line9

And I want to print it across 3 columns with this structure so that first column has lines 1-2-3, second column has the lines 4-5 and the last column has the lines 6-7-8-9 (or the rest of the file):

line1 line4 line6
line2 line5 line7
line3       line8
            line9

Essentially I want to print a file in a way that the output is distributed to N columns and every column has predetermined (and possibly different from each other) amount of lines. If possible, I want to preserve leading/trailing spaces in lines. How can I do that?

I fiddled around with columns and pr, but I couldn't even come close.

  • Something like this could work: pr -m <(sed -n 1,3p file) <(sed -n 4,5p file) <(sed -n 6,9p file) – pLumo Oct 5 '18 at 12:47
  • That solves my problem, thanks! Maybe add as an answer so I can accept it? (And can you explain how it works?) – isamert Oct 5 '18 at 12:55
3

You could use the merge functionality of pr, which merges multiple files to columns.

Something like this would work:

pr -J -m <(sed -n 1,3p file) <(sed -n 4,5p file) <(sed -n 6,9p file)
0

Another option is using paste:

paste <(sed '1,3!d' infile) <(sed '4,5!d' infile) <(sed '6,$!d' infile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.