Remove newline character between two delimiters

hi i am having delimited .dat file having content like below.

test.dat(5 line of records)

PT2~Stag~Pt2 Stag Test.
Updated~PT2 S T~Area~~UNCEF R20~~2012-05-24 ~2014-05-24~~
PT2~Stag y~Pt2 Stag Test.
Updated~PT2 S T~Area~METR~~~2012-05-24~2014-05-24~~test
PT2~Pt2 Stag Test~~PT2 S T~Area~~UNCEFACT R20~true~2012-05-24~2014-05-24~~

sometime we are getting newline character in between data(like its there in 1st 2 lines hence it get spilted into 4).we want newline char only at end of line. if any new line character coming in between then want to replace with space.each line have 11 delimiters.

expected output :(3 line)

PT2~Stag~Pt2 Stag Test.Updated~PT2 S T~Area~~UNCEF R20~~2012-05-24 ~2014-05-24~~
PT2~Stag y~Pt2 Stag Test.Updated~PT2 S T~Area~METR~~~2012-05-24~2014-05-24~~
PT2~Pt2 Stag Test~~PT2 S T~Area~~UNCEFACT R20~true~2012-05-24~2014-05-24~~

can you please suggest any command for that.
thanks in advance!!!!!

perl -pe '/^PT/ && chomp' test.dat

Something like this?

awk '!/^~$/{printf $0; next}1' file

Try:

awk -F~ '{while(NF<12 && getline p)$0=$0 p}1' infile

Thanks alot for your quick reply. :):slight_smile:

@balajesuri
--this command is working fine if file have upto 3 expected output records

@Scrutinizer
really its amezing!!!!

Thanks,
sunil