I have a multiline file where the records are separated by a new line (
)
.
Each record split is identified by some text break
.
How can I use awk so that if the record begins or ends with break
it joins it onto the previous or next line, whilst retaining the record separator.
Input ($
represents EOL). Multiple breaks;
can be ignored or treated as one:
A| break;$
B| break;$
C| break;$
D$
E|$
break; FGH|$
break; IJ| break;$
KLM| break;
NOP$
Desired output:
A|B|C|D$
E|FGH|IJ|KLM|NOP$
Current code (works on end break;
but no join the lines beggining with break
onto the previous one:
awk '{if (sub(/break;$/,"")) printf "%s", $0; else if (sub(/^break;/,"")) printf $0,"%s"; else print $0}' myfile
I suspect the problem is in the else if part but I cant figure out the correct syntax to join onto the previous line if the line begins with break;
.
Any help would be appreciated but please consider awk
solutions only.
UPDATE:
Thank you everyone who contributed! The suggestions below work, but it seems some records with consecutive break;
in them are still causing problems:
A| break;
B| break;
C| break; break;
break; D$
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…