Condition based concatenation.

Hello,

I am looking for concatenating the lines based on conditions. Below are the contents of the file:

Infile:
-----
Test1.PO_Itm COLUMN GAC_DT.
Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD).
Test1.PO_Itm COLUMN PLNTCD.
Test1.PO_Itm COLUMN ACTVIND.
Test2.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.
Test2.RgnToTerrtryGPI COLUMN RGNID.
Test1.ProdtAtPlnt COLUMN (PARTITION
,
PRODTCD ,PLNTIDCD).
Test3.PO_Itm COLUMN GAC_DT.
Test3.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.

CONDITION:
----------
If a line does not start with either Test1 OR Test2 OR Test3 then concatenate that line to previuos line else do nothing. The desired output should be (changed lines marked with *)

OUTPUT:
--------
Test1.PO_Itm COLUMN GAC_DT.
Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD).
Test1.PO_Itm COLUMN PLNTCD.
Test1.PO_Itm COLUMN ACTVIND.
*Test2.RgnToTerrtryGPI COLUMN RGNTOTERRTRYENDDT.
Test2.RgnToTerrtryGPI COLUMN RGNID.
*Test1.ProdtAtPlnt COLUMN (PARTITION,PRODTCD ,PLNTIDCD).
Test3.PO_Itm COLUMN GAC_DT.
*Test3.RgnToTerrtryGPI COLUMN RGNTOTERRTRYENDDT.

Thank You in advance.

Try...

awk '/^Test[123]/&&NR>1{printf ORS}{printf $0}END{printf ORS}' file1 > file2
my $tmp="";
while(<DATA>){
  chomp;
  if(/^Test[123]/){
   if($tmp ne ""){
     print $tmp,"\n";
     $tmp=$_;
   }
   else{
    $tmp=$_;
   }
  }
  else{
   $tmp=$tmp.$_;
  }
  $flag=0 if ! /^Test[123]/;
}
print $tmp if $flag != 1;
__DATA__
Test1.PO_Itm COLUMN GAC_DT.
Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD).
Test1.PO_Itm COLUMN PLNTCD.
Test1.PO_Itm COLUMN ACTVIND.
Test2.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.
Test2.RgnToTerrtryGPI COLUMN RGNID.
Test1.ProdtAtPlnt COLUMN (PARTITION
,
PRODTCD ,PLNTIDCD).
Test3.PO_Itm COLUMN GAC_DT.
Test3.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.

Hi Ygor,

Thanks for the code.

This code worked for me perfectly for the scenario I gave. I may have scenario where in I would not have Test in common like

Test1.PO_Itm COLUMN ACTVIND.
DB.RgnToTerrtryGPI COLUMN
RGNTOTERRTRYENDDT.

So now the condition will be
If a line does not start with either Test1 OR DB then concatenate that line to previuos line else do nothing. The desired output should be (changed lines marked with *)

Test1.PO_Itm COLUMN ACTVIND.
DB.RgnToTerrtryGPI COLUMN RGNTOTERRTRYENDDT.

Also could you please explain the code for me.

Thanks.

Hi Summer cherry,

Thanks for your time in putting the code for me.