Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 353
janeruthh Avatar asked Dec 17 '11 14:12

janeruthh


2 Answers

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

like image 79
Zsolt Botykai Avatar answered Nov 16 '22 00:11

Zsolt Botykai


The paste command can do this. Its "-s" option will join consecutive lines; and the "-d" option specifies a list of characters to use as delimiters, repeating them cyclically. Join first with a space, then with a newline, and repeat:

seq 10 | paste -sd" \n" -
like image 33
Bob Lied Avatar answered Nov 16 '22 01:11

Bob Lied