awk / shell - Fix broken lines and data

Gurus,
I am struggling with a issue and thought I could use some of your expertise.

Need Help with this

I have a flat file that has millions of records

24|john|account ~ info |56|
25|kuo|account ~ journal |58|
27|kim|account ~ journal |59|
28|San|account ~
journal |60|
29|jay|account ~ journal |26|
29|Nan|account ~
journal |66|
30|Kee|account ~ journal |27|

Need the following

  • Replace the ~ with space ' ' from the flat file
  • Join the broken lines together.
    All lines end with a pipe "|" delimiter

Required Output

24|john|account info |56|
25|kuo|account journal |58|
27|kim|account journal |59|
28|San|account journal |60|
29|jay|account journal |26|
29|Nan|account journal |66|
30|Kee|account journal |27|

Thanks,

Try...

awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2

It worked fine.

Its almost mid night and i was fire fighting this .. Finally get to sleep now

Thanks a lot ... for the quick reply !!!

awk '{sub(/~ */,x);printf $0(/\|$/?ORS:x)}'
awk '{sub(/~ */,x);sub(/\|$/, "|\n")}8' ORS="" file1 >file2