AWK Script - Print a column - within a Row Range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Script - Print a column - within a Row Range
# 1  
Old 09-26-2011
Question 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:
Code:
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
Code:
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
Code:
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
Code:
<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
Code:
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

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

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 09-27-2011 at 03:21 AM..
# 2  
Old 09-26-2011
Quote:
Originally Posted by panapty
Hi, Please read the whole thread.
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...
# 3  
Old 09-26-2011
Ok , I mean the whole post!!!
# 4  
Old 09-26-2011
This does what you asked but the code should be re-written from scratch as it's not very pretty:

Code:
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)
  }
}}

# 5  
Old 09-27-2011
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?


Code:
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,,,,

Code:
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

# 6  
Old 09-27-2011
Oh, without the [code] and [/code] tags the input file looked different.

This updated version should work:

Code:
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)
     }
 }
}


Last edited by Chubler_XL; 09-27-2011 at 05:39 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 09-28-2011
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
Code:
67807532,714011000643614,OBO-0,OBR-2,OBI-0,TS21-1,OICK-24,TICK-11,1,2

Input File
Code:
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
Code:
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)
     }
 }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to do column to row in awk

Hi , Can anyone help me suggesting - how to do the below trick with awk Input 120 130 140 210 310 410 645 729 800 Output 120 130 140 (6 Replies)
Discussion started by: Indra2011
6 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

awk script row to column

Hi.. I have data : Report testing1 20180419 08:00 Report testing2 20180419 07:35 Report testing 20180419 08:01 Source = data1 Report testing4 20180419 08:05 Source = data1 Report testing5 20180419 08:10 Source = data2 Report testing6 20180419 08:01 Report testing7 20180419 08:19... (4 Replies)
Discussion started by: buncit8
4 Replies

4. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

8. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

9. Shell Programming and Scripting

Print unique names in each row of a specific column using awk

Is it possible to remove redundant names in the 4th column? input cqWE 100 200 singapore;singapore AZO 300 400 brazil;america;germany;ireland;germany .... .... output cqWE 100 200 singapore AZO 300 400 brazil;america;germany;ireland (4 Replies)
Discussion started by: quincyjones
4 Replies

10. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies
Login or Register to Ask a Question