How to use Perl to merge multi-line into single line

Hi,

Can anyone know how to use perl to merge the following multi-line information which beginning with "BAM" into one line. For each line need to delete the return and add a space. Please see the red color line.

*****Org. Multi-line)
BAM admin 101.203.57.22 2059 2009-03-27 07:20:30+08:00 go 0 PP 2009-03-27 07:20:30+08:00 LOCAL
abc LST TRALIC:;
BAM ljha1 101.203.57.23 2059 2009-03-27 07:20:30+08:00 go 0 PP 2009-03-27 07:20:30+08:00 LOCAL
abc LST TRALIC:;
BAM comm 101.204.4.82 5023 2009-03-27 07:16:48+08:00 go 0 PP 2009-03-27 07:16:54+08:00 EMSOP
abc /*76518799
/LST TRFIINF:TA=ALL;

Merge to single line
BAM admin 101.203.57.22 2059 2009-03-27 07:20:30+08:00 go 0 PP 2009-03-27 07:20:30+08:00 LOCAL abc LST TRALIC:;
BAM ljha1 101.203.57.23 2059 2009-03-27 07:20:30+08:00 go 0 PP 2009-03-27 07:20:30+08:00 LOCAL abc LST TRALIC:;
BAM comm 101.204.4.82 5023 2009-03-27 07:16:48+08:00 go 0 PP 2009-03-27 07:16:54+08:00 EMSOP abc /*76518799
/LST TRFIINF:TA=ALL;

Thank You

HappyDay

if each line are extended to two lines, can use below simple code:

cat a.txt | paste - -
undef $/;
open $fh,"<","a.txt";
$str=<$fh>;
$str=~s/\n/ /g;
@arr=split("BAM",$str);
map { print "BAM $_\n" if $_} @arr;

Cherry,

Thank you very much !!!
:b::slight_smile:

HappyDay

Hello,

How about can we remove the space and also add the | between? (like the red color output

Org. Multi-line
BAM admin 101.203.57.22 2059 2009-03-27 07:20:30+08:00 go 0 PP 2009-03-27 07:20:30+08:00 LOCAL
abc LST TRALIC:;
BAM ljha1 101.203.57.23 2059 2009-03-27 07:20:30+08:00 go 0 PP 2009-03-27 07:20:30+08:00 LOCAL
abc LST TRALIC:;
BAM comm 101.204.4.82 5023 2009-03-27 07:16:48+08:00 go 0 PP 2009-03-27 07:16:54+08:00 EMSOP
abc /*76518799
/LST TRFIINF:TA=ALL;

*Merge to single line
BAM admin|101.203.57.22 2059|2009-03-27 07:20:30+08:00|go|0|PP 2009-03-27|07:20:30+08:00|LOCAL|abc|LST TRALIC:;

Thank You

HappyDay