Recently, I asked a question how to sort pairs of lines, and one of the answers suggested concatenating lines with sed
, like this:
cat file.txt | sed -n 'N;s/\n//;p' | sort -t";" -k43,43n | perl -F';' -ane '$,=";";print @F[0..13],"\n";print @F[14..$#F]'
which worked brilliantly, but now my problem generalized to sorting n-tupels of lines, which I can't figure out how to do with sed.
Everything I found was either for 2 lines or ALL lines, but I need n lines (where n is 5 at the moment, but a general way would be great).
Bonus points for a nice way of rewriting the perl
part to accomodate n lines, but the problem is really about the sed
part.
I am also not hung on sed
specifically, so if you have a nice solution using a different command line tool, please post it.
Update: Example input (n == 3)
a1;b1;c1;
n1;m1;l1;
d1;e1;f1;g1
n2;m2;l2;
a2;b2;c2;
d2;e2;f2;g2
Best Answer
That will concatenate 5 lines - or 1 + 4 lines - replacing each newline with a single space. However:
...would also work.
Your
g
sort thing could work like:...which would be a fairly general way of doing it, though it relies on there being no spaces in the input file. it joins every two lines on a space, the copies the last
;
split field to the head of each line, sorts on the first field, then cuts it away and splits the lines back out.