AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread.
I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well.

example:

PACKET DATA PROTOCOL CONTEXT DATA 
APNID   PDPADD                EQOSID  VPAA  PDPCH    PDPTY  PDPID
   10                            8    NO    8-0      IPV4    1
    1                            8    NO    8-0      IPV4    2
    2                            8    NO    8-0      IPV4    3
END

What I need to get is the first column between the pattern
PACKET DATA PROTOCOL CONTEXT DATA and
END

for this case , that will be
OUPUT

63728153,714011000635895,OBO-0,OBR-2,OBI-0,TS21-1,OICK-30,TICK-31,10,1,2

I have copied the output from the script.
****************************************
SCRIPT

BEGIN { RS="hgsdp" }
{  cat[1] = "OBO"
   cat[2] = "OBR"
   cat[3] = "OBI"
   cat[4] = "TS21"
}
{
        if ($12 ~ /NOT/ ) printf "\n" substr ($1,16,8) "," "NOT_CONNECTED" ;
        else printf "\n" substr ($1,16,8) "," $12
}
{
        for (x = 1; x <= 4; x++)
        if (match($0,cat[x]))  printf "," substr($0, RSTART, RLENGTH+2)
        else {  printf  "," cat[x]"-0" }
        if (match($0,"OICK"))  printf "," substr($0, RSTART, RLENGTH+3)
        if (match($0,"TICK"))  printf "," substr($0, RSTART, RLENGTH+3)
}

****************************************
INPUT FILE

<hgsdp:msisdn=50763728153,all;
HLR SUBSCRIBER DATA
SUBSCRIBER IDENTITY
MSISDN           IMSI             STATE          AUTHD
50763728153      714011000635895  CONNECTED      AVAILABLE
PERMANENT SUBSCRIBER DATA
SUD
CAT-10       DBSG-1       TSMO-0       OBR-2
OSB4-1       TS11-1       TS21-1       TS22-1
BS2G-1       BS3G-1       REDMCH-1     OFA-1
PWD-0000     EMLPP-1      DEMLPP-5     MEMLPP-5
OICK-30      TICK-31      CFU-1        BAOC-1
BOIC-1       BOIEXH-1     BAIC-1       BICRO-1
CAW-1        SOCFU-0      SOCB-1       SOCLIP-0
SODCF-0      SOSDCF-7     HOLD-1       MPTY-1
CLIP-1
SCHAR-8-0
AMSISDN            BS       BC
NONE
PACKET DATA PROTOCOL CONTEXT DATA
APNID   PDPADD                EQOSID  VPAA  PDPCH    PDPTY  PDPID
   10                            8    NO    8-0      IPV4    1
    1                            8    NO    8-0      IPV4    2
    2                            8    NO    8-0      IPV4    3
END
<hgsdp:msisdn=50763780269,all;

****************************************
OUPUT

63728153,714011000635895,OBO-0,OBR-2,OBI-0,TS21-1,OICK-30,TICK-31
63780269,714011000639632,OBO-0,OBR-2,OBI-0,TS21-1,OICK-10,TICK-18

****************************************

What whole thread? You created a whole new one.

If this was supposed to be in a thread, you might want to find it and post it there...

Ok , I mean the whole post!!!

This does what you asked but the code should be re-written from scratch as it's not very pretty:

BEGIN { RS="hgsdp" }
{ cat[1] = "OBO"
  cat[2] = "OBR"
  cat[3] = "OBI"
  cat[4] = "TS21"
}
{
if ($12 ~ /NOT/ ) printf "\n" substr ($1,16,8) "," "NOT_CONNECTED" ;
else printf "\n" substr ($1,16,8) "," $12
}
{
for (x = 1; x <= 4; x++)
if (match($0,cat[x])) printf "," substr($0, RSTART, RLENGTH+2)
else { printf "," cat[x]"-0" }
if (match($0,"OICK")) printf "," substr($0, RSTART, RLENGTH+3)
if (match($0,"TICK")) printf "," substr($0, RSTART, RLENGTH+3)
if (match($0,"PACKET.*END")) {
  A=substr($0, RSTART, RLENGTH)
  while(match(A, "\n[0-9]* ")) {
      printf ","substr(A, RSTART+1, RLENGTH-2)
      A=substr(A,RSTART+RLENGTH+1)
  }
}}

thanks Chubler_XL for taking your time and help me out with this.
Unfortunately, the input file is a little bit tricky. It seems that is trying to look for the numbers, but only displays the placeholder and not the value.

I have been playing around with the values of RSTART and RLENGTH with no luck. Any suggestion?

67807532,CONNECTED,OBO-0,OBR-2,OBI-0,TS21-1,OICK-24,TICK-11,,,,
67807543,CONNECTED,OBO-0,OBR-2,OBI-0,TS21-1,OICK-24,TICK-11,,,,
67807581,CONNECTED,OBO-0,OBR-2,OBI-0,TS21-1,OICK-24,TICK-11,,,,
PACKET DATA PROTOCOL CONTEXT DATA
APNID   PDPADD                EQOSID  VPAA  PDPCH    PDPTY  PDPID
    1                            8    NO    4-0      IPV4    1
    2                            8    NO    4-0      IPV4    2
END

Oh, without the

```text
 and 
```

tags the input file looked different.

This updated version should work:

BEGIN { RS="hgsdp" }
{   cat[1] = "OBO"
    cat[2] = "OBR"
    cat[3] = "OBI"
    cat[4] = "TS21"
}
{
    if ($12 ~ /NOT/ ) printf "\n" substr ($1,16,8) "," "NOT_CONNECTED" ;
    else printf "\n" substr ($1,16,8) "," $12
}
{
    for (x = 1; x <= 4; x++)
    if (match($0,cat[x])) printf "," substr($0, RSTART, RLENGTH+2)
    else { printf "," cat[x]"-0" }
    if (match($0,"OICK")) printf "," substr($0, RSTART, RLENGTH+3)
    if (match($0,"TICK")) printf "," substr($0, RSTART, RLENGTH+3)
    if (match($0,"PACKET.*END")) { A=substr($0, RSTART, RLENGTH)
     while(match(A, "\n *[0-9]+ ")) {
         V=substr(A, RSTART+1, RLENGTH-2)
         sub(/^ */, ",",V)
         printf V
         A=substr(A,RSTART+RLENGTH+1)
     }
 }
}
1 Like

Chubler_XL
Man that was awesome, It worked really cool.. Thanks again for your time and bearing with me this script!!!!
Saludos.

---------- Post updated at 07:54 AM ---------- Previous update was at 07:37 AM ----------

One more help, Last time I did not realize about some categories might display several times in the input file. For example, category BAOC, it has 4 occurrences, so I need to extract that category and the column beside called STATUS separated by commas

OUTPUT

67807532,714011000643614,OBO-0,OBR-2,OBI-0,TS21-1,OICK-24,TICK-11,1,2

Input File

hgsdp:msisdn=50767807532,all;
HLR SUBSCRIBER DATA
SUBSCRIBER IDENTITY
MSISDN           IMSI             STATE          AUTHD
50767807532      714011000643614  CONNECTED      AVAILABLE
NAM
0
PERMANENT SUBSCRIBER DATA
SUD
CAT-10       DBSG-1       TSMO-0       OBR-2
OSB4-1       TS11-1       TS21-1       TS22-1
BS2G-1       BS3G-1       REDMCH-1     OFA-1
PWD-0000     EMLPP-1      DEMLPP-5     MEMLPP-5
OICK-24      TICK-11      CFU-1        BAOC-1
BOIC-1       BOIEXH-1     BAIC-1       BICRO-1
CAW-1        SOCFU-0      SOCB-1       SOCLIP-0
SODCF-0      SOSDCF-7     HOLD-1       MPTY-1
CLIP-1       RED-1
SCHAR-8-0
SUPPLEMENTARY SERVICE DATA
BSG
TS10
SS       STATUS        FNUM                 TIME
                       SADD
BAIC     NOT ACTIVE
BAOC     NOT ACTIVE
BSG
TS20
SS       STATUS        FNUM                 TIME
                       SADD
BAIC     NOT ACTIVE
BAOC     NOT ACTIVE
BSG
BS20
SS       STATUS        FNUM                 TIME
                       SADD
BAIC     NOT ACTIVE
BAOC     NOT ACTIVE
BSG
BS30
SS       STATUS        FNUM                 TIME
                       SADD
BAIC     NOT ACTIVE
BAOC     NOT ACTIVE

Script

BEGIN { RS="hgsdp" }
{   cat[1] = "OBO"
    cat[2] = "OBR"
    cat[3] = "OBI"
    cat[4] = "TS21"
    cat[5] = "BAOC"
}
{
    if ($12 ~ /NOT/ ) printf "\n" substr ($1,16,8) "," "NOT_CONNECTED" ;
    else printf "\n" substr ($1,16,8) "," $12
}
{
    for (x = 1; x <= 5; x++)
    if (match($0,cat[x])) printf "," substr($0, RSTART, RLENGTH+2)
    else { printf "," cat[x]"-0" }
    if (match($0,"OICK")) printf "," substr($0, RSTART, RLENGTH+3)
    if (match($0,"TICK")) printf "," substr($0, RSTART, RLENGTH+3)
    if (match($0,"PACKET.*END")) { A=substr($0, RSTART, RLENGTH)
     while(match(A, "\n *[0-9]+ ")) {
         V=substr(A, RSTART+1, RLENGTH-2)
         sub(/^ */, ",",V)
         printf V
         A=substr(A,RSTART+RLENGTH+1)
     }
 }
}