Merging Lines based on criteria

Hello,

Need help with following scenario.

A file contains following text:

{beginning of file}
New: This is a new record and it is not
on same line. Since I have lost touch with script
take this challenge and bring all this in one line.
New: Hello losttouch. You seem to be struggling with 
this really simple challenge.
New: It is better that I ask some expert on unix.com
They will show you this is simple enough.
{end of file}

What I am trying to accomplish is to get all the lines starting from "New:" until next "New:" is read; on to one line. Resultant lines should be like:

New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line.
New: Hello losttouch. You seem to be struggling with this really simple challenge.
New: It is better that I ask some expert on unix.com.They will show you this is simple enough.

As I mentioned in the above statements, I have lost touch with this, I am really struggling to accomplish this task. Any help is greatly appreciated.

Thanks so much.

EDIT: Needless to say that if I read next "New:" (lets say in any variable) it implies, my previous record is complete and should be displayed or appended to a file.

Welcome to the forum.

perl -ne 'chomp; if(/^New:/){print "\n$_ ";$f=1}elsif(!/^New:/ && $f==1){print "$_ "}' inputfile

A different logic:

perl -ne 'chomp; if(/^New:/){print "$x\n"; $x="$_ "}else{$x .= "$_ "} END {print "$x\n"}' inputfile
1 Like
awk 'NR>1{gsub(/^New/,"\nNew")}END{printf "\n"}1' ORS=' ' infile
1 Like
tr '\n' ' ' < infile | sed 's/ New/\nNew/g'
1 Like

Thank you everyone for the help! It is greatly appreciated. I used the awk statement and it worked really well. One edit I made to it was by changing the ORS from ' ' to '' (no space). I had to remove space because it was introduced space in and was making the entries less "meaningful".

Thanks once again!