Output on one line using awk or sed

I have a file of 100,000 lines in the below format:

answer.bed

chr1    957570    957852    
     NOC2L
chr1    976034    976270     
     PERM1
chr1    976542    976787    
     PERM1 

I need to get each on one line and so far what I have tried doesn't seem to be working. Thank you :).

Desired output:

chr1    957570    957852     NOC2L
chr1    976034    976270     PERM1
chr1    976542    976787     PERM1 

I have tried:

 sed 'N;s/\n//' answer.bed > output.bed 

output.txt

chr1    957570    957852     NOC2Lchr1    976034    976270     PERM1
chr1    976542    976787     PERM1chr1    978907    979122     PERM1
chr1    979192    979413     PERM1chr1    979478    979647     PERM1  
 awk 'NR%2{printf $0" ";next;}1' answer.bed > output.bed 

output.txt

chr1    957570    957852     NOC2L chr1    976034    976270     PERM1
chr1    976542    976787     PERM1 chr1    978907    979122     PERM1
chr1    979192    979413     PERM1 chr1    979478    979647     PERM1 

I can't reproduce your problem; both code snippets work as expected. You had problems originating from using DOS/<CR> terminators before; could that be a possible reason?

2 Likes
xargs -n4 <answer.bed >output.bed
# cat /tmp/myt
chr1    957570    957852
     NOC2L
chr1    976034    976270
     PERM1
chr1    976542    976787
     PERM1
# xargs -n4 </tmp/myt
chr1 957570 957852 NOC2L
chr1 976034 976270 PERM1
chr1 976542 976787 PERM1
#
1 Like

I will remove the DOS/<CR> terminators before;, that is it as the original file was from excel. Thank you :).