How do I join pairs of consecutive lines in a large file (1 million lines) using vim, sed, or another similar tool?

janeruthh picture janeruthh · Dec 17, 2011 · Viewed 10.9k times · Source

I need to move the contents of every second line up to the line above such that line2's data is alongside line1's, either comma or space separated works.

Input:

line1
line2
line3
line4

Output:

line1 line2
line3 line4

I've been doing it in vim with a simple recording but vim seems to crash when I tell it to do it 100 000 times... I'm thinking maybe sed would be a good alternative but not sure how to do what I want or maybe there's a better option?

Each line only contains 1 numerical value, I just have a million lines...

Answer

Zsolt Botykai picture Zsolt Botykai · Dec 17, 2011

If I understand correctly, you have:

line1 
line2
line3
line4
...

and you want:

line1<SEP>line2
line3<SEP>line4

then you can do it easily with (g)awk like this:

awk 'NR % 2 == 1 { o=$0 ; next } { print o "<sep>" $0 }' INPUTFILE

See it in action here.

Update: if the number of lines is odd, the above will omit the last line (as Martin Stettner pointed out) so this will not:

awk 'NR % 2 == 1 { o=$0 ; next } { print o "<sep>" $0 } END { if ( NR % 2 == 1 ) { print o } }' INPUTFILE

HTH