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...
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
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" -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With