Transpose lines from individual blocks to unique lines

Hello to all, happy new year 2013!

May somebody could help me, is about a very similar problem to the problem I've posted here where the member rdrtx1 and bipinajith helped me a lot.

It is very similar, the difference is that instead to have the command MGISP:IMSIS=XXX; for many series, I have
MGISP:IMSIS=ALL; !NODE NAME! for each node. The structure of the blocks are the same.

I would like to transpose the data of each series in input file for each node, count the series for each node (in 1rst field), and
put the name of the node for each series (in 2nd field). The rest of parameters would go from field 3 to 18. Please see the input sample below:

The solution posted by rdrtx1 works perfect, but I don't know how to modify it in order to get these changes.

Note:
The parameters OWNMS, NATMS and STALL don't have a number related, so in the output could be written a "1", to show that
they appear in the input.

Thanks in advance for any help.

Regards.

This input is:

<MGISP:IMSIS=ALL;!MGCPTS 13!
MT IMSI NUMBER SERIES ANALYSIS DATA
OPERATING TABLE
IMSIS            M                   NA  ANRES
24569929         6-77182             4   OBA-200
                                         BO-200
                                         PLMN-0
                                         MAPVER-2
                                         INOPER-121
                                         NRRG-0
                                         CBA-58
                                         CBAZ-58

33342            5-44531             4   OBA-60
                                         BO-35
                                         OWNMS
                                         NATMS
                                         ERIS-0
                                         PLMN-0
                                         STALL
                                         MAPVER-2
                                         INOPER-120
                                         NRRG-0
                                         CBA-15
                                         CBAZ-15
                                         CAMEL-3

40981            5-38889             4   OBA-200
                                         BO-200
                                         PLMN-0
                                         MAPVER-2
                                         NRRG-0
                                         CBA-18
                                         CBAZ-18
                                         CAMEL-0

END
<MGISP:IMSIS=ALL; !MGCKLI 02!
MT IMSI NUMBER SERIES ANALYSIS DATA
OPERATING TABLE
IMSIS            M                   NA  ANRES
942111           6-33313             4   OBA-150
                                         BO-49
                                         PLMN-0
                                         MAPVER-2
                                         NRRG-0
                                         CBA-22
                                         CBAZ-22
                                         CAMEL-3
 
942123           6-889134            4   OBA-150
                                         BO-49
                                         PLMN-0
                                         MAPVER-2
                                         NRRG-0
                                         CBA-22
                                         CBAZ-22
 
END
<MGISP:IMSIS=ALL;!MGCLS-03!
MT IMSI NUMBER SERIES ANALYSIS DATA
OPERATING TABLE
IMSIS            M                   NA  ANRES
2556798          5-44770             4   OBA-150
                                         BO-49
                                         PLMN-0
                                         MAPVER-2
                                         NRRG-0
                                         CBA-67
                                         CBAZ-67
                                         CAMEL-2

END

and desired output is:

#,NODE,IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL
1,MGCPTS 13,24569929,6-77182,4,200,200,0,2,0,58,58,,121,,,,
2,MGCPTS 13,33342,5-44531,4,60,35,0,2,0,15,15,3,120,1,1,0,1
3,MGCPTS 13,40981,5-38889,4,200,200,0,2,0,18,18,0,,,,,
1,MGCKLI 02,942111,6-33313,4,150,49,0,2,0,22,22,3,,,,,
2,MGCKLI 02,942123,6-889134,4,150,49,0,2,0,22,22,,,,,,
1,MGCLS-03,2556798,5-44770,4,150,49,0,2,0,67,67,2,,,,,

You could try a quick adaptation of rdrtx1's code:

awk '
BEGIN {
  z="#,NODE";
  s="IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL";
  k=split(s,h,","); print z "," s;
}
$1 == "END" && c==0 {
  a[h[1]]=v; a[h[2]]="UNIDENTIFIED";
  printf ++cn[node] "," node ",";
  for (i=1; i<=k; i++) printf  (i<k) ? a[h]",":a[h]"\n";
  delete a;
}
!$1 && c==1 {
  if (a[h[1]]) {
    printf ++cn[node] "," node ",";
    for (i=1; i<=k; i++) printf  (i<k) ? a[h]",":a[h]"\n";
  }
  delete a;
}
/<..*;/ {++n; c=0; j=split($0,o,"[:=;!]"); w=o[j-4]; v=o[j-3]; node=o[j-1]}
!$1 && c==0 {next}
$1 && c==1 {
  if (NF>1) for (i=1; i<=NF-1; i++) a[h]=$i;
  f=v=$NF; x=sub("[-].*","", f); x=sub(".*[-]","", v);
  if (x) {a[f]=v} else {a[f]=1};
}
$1 == w {c=1}
' infile

Hello Scrutinizer,

That was all needed? jaja

Few changes, I thought was needed many more things!

Thank youu for your great help!!!.

Best regards

Hi, it was just a quick fix to give you an idea. However, I think this code is difficult to maintain. If it is not a one time thing is best to try to figure out its structure and then write your own code.