Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine odd and even numbered lines?

Tags:

text

I want to combine every even-numbered line with the line above it. Something like:

Line one,csv,csv,csv
Line two,csv,csv
Line three,csv,csv,csv,csv
Line four,csv

The result should look like this:

Line one,csv,csv,csv,Line two,csv,csv
Line three,csv,csv,csv,csv,Line four,csv

Any ideas how to achieve that in either Perl or sed/awk?

like image 464
ery Avatar asked Oct 20 '11 20:10

ery


1 Answers

here it is, with sed:

sed '$!N;s/\n/,/'

and with awk:

awk '{if (e) {print p","$0;} else {p=$0;} e=!e;}'

or

awk 'NR%2==0 {print p","$0;} NR%2 {p=$0;}'
like image 189
Karoly Horvath Avatar answered Oct 03 '22 23:10

Karoly Horvath