Map values of blocks in a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Map values of blocks in a single line
# 1  
Old 12-26-2012
Map values of blocks in a single line

Hello to all in forum,

Maybe some awk expert could help me.

I have this sample input:
Code:
<MGISP:IMSIS=99995626;
MT IMSI NUMBER SERIES ANALYSIS DATA
OPERATING TABLE
IMSIS            M                   NA  ANRES
99995626  6-77182                    4   OBA-200
                                         BO-200
                                         PLMN-0
                                         MAPVER-2
                                         INOPER-121
                                         NRRG-0
                                         CBA-58
                                         CBAZ-58

END
<MGISP:IMSIS=80321;
MT IMSI NUMBER SERIES ANALYSIS DATA
OPERATING TABLE
IMSIS            M                   NA  ANRES
80321            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

END
<MGISP:IMSIS=29783;
MT IMSI NUMBER SERIES ANALYSIS DATA
OPERATING TABLE
IMSIS            M                   NA  ANRES
29783            5-38889             4   OBA-200
                                         BO-200
                                         PLMN-0
                                         MAPVER-2
                                         NRRG-0
                                         CBA-18
                                         CBAZ-18
                                         CAMEL-0

END
<MGISP:IMSIS=53002; 
MT IMSI NUMBER SERIES ANALYSIS DATA

OPERATING TABLE

UNIDENTIFIED NUMBER SERIES

END

And I'm would like to have the values of each IMSIS group mapped in a single line, so for the 3 IMSIS blocks
above, the output would be 3 lines as follow (the headers are fixed and could go in that order):

Code:
IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL                     
99995626,6-77182,4,200,200,0,2,0,58,58,,121,,,,
80321,5-44531,4,60,35,0,2,0,15,15,3,120,1,1,0,1
29783,5-38889,4,200,200,0,2,0,18,18,0,,,,,
53002,UNIDENTIFIED

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 (I've highlighted those 1's in red for reference).

If in the input some IMSIS is not defined (Print shows UNIDENTIFIED), then in the output would show UNIDENTIFIED.

Thanks in advance for any help.

Last edited by Ophiuchus; 12-26-2012 at 08:05 PM..
# 2  
Old 12-27-2012
Code:
awk  'BEGIN {
 print "IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL"
 v1=v2=v3=v4=v5=v6=v7=v8=v9=v10=v11=v12=v13=v14=v15=v16=""; }
  /IMSIS=/ { split($0,arr,"="); v1=arr[2]; gsub(";","",v1); }
  NF==4 && !/IMSIS/ { v2=$2; v3=$3; split($4,arr,"-"); v4=arr[2]; }
  NF==1 && /BO/ { if($0 ~ /-/) { split($0,arr,"-"); v5=arr[2]; } else v5=1; }
  NF==1 && /PLMN/ { if($0 ~ /-/) { split($0,arr,"-"); v6=arr[2]; } else v6=1; }
  NF==1 && /MAPVER/ { if($0 ~ /-/) { split($0,arr,"-"); v7=arr[2]; } else v7=1; }
  NF==1 && /NRRG/ { if($0 ~ /-/) { split($0,arr,"-"); v8=arr[2]; } else v8=1; }
  NF==1 && /CBA/ { if($0 ~ /-/) { split($0,arr,"-"); v9=arr[2]; } else v9=1; }
  NF==1 && /CBAZ/ { if($0 ~ /-/) { split($0,arr,"-"); v10=arr[2]; } else v10=1; }
  NF==1 && /CAMEL/ { if($0 ~ /-/) { split($0,arr,"-"); v11=arr[2]; } else v11=1; }
  NF==1 && /INOPER/ { if($0 ~ /-/) { split($0,arr,"-"); v12=arr[2]; } else v12=1; }
  NF==1 && /OWNMS/ { if($0 ~ /-/) { split($0,arr,"-"); v13=arr[2]; } else v13=1; }
  NF==1 && /NATMS/ { if($0 ~ /-/) { split($0,arr,"-"); v14=arr[2]; } else v14=1; }
  NF==1 && /ERIS/ { if($0 ~ /-/) { split($0,arr,"-"); v15=arr[2]; } else v15=1; }
  NF==1 && /STALL/ { if($0 ~ /-/) { split($0,arr,"-"); v16=arr[2]; } else v16=1; }
  /END/ {
   if(v2=="")
      printf ("%s,%s\n",v1,"UNIDENTIFIED");
   else printf ("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16);
  v1=v2=v3=v4=v5=v6=v7=v8=v9=v10=v11=v12=v13=v14=v15=v16="";
}' filename

# 3  
Old 12-27-2012
Hello bipinajith, thank you for your help!

It works fine, just one issue. If a particular series has more than one block that begins with the same numbers, the print is like below:
In this example, the series 942 has 3 blocks of IMSIs related, so the print only for this command MGISP:IMSIS=942 is:
Code:
<MGISP:IMSIS=942;
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
 
94201            5-777714            4   OBA-150
                                         BO-49
                                         PLMN-0
                                         MAPVER-2
                                         NRRG-0
                                         CBA-50
                                         CBAZ-50
                                         CAMEL-0
 
END

And the output should be:
Code:
IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL
942111,6-33313,4,150,49,0,2,0,22,22,3,,,,,
942123,6-889134,4,150,49,0,2,0,22,22,,,,,,
94201,5-777714,4,150,49,0,2,0,50,50,0,,,,,

But now is only printing the first line and with 942 only as below:
Code:
IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL
942,6-33313,4,150,49,0,2,0,22,22,3,,,,,

I think the first 4 fields could be get from lines in red.

May you help me to fix this part.
Thanks in advance.
# 4  
Old 12-27-2012
This is an ugly awk code, but I think it will work:
Code:
awk  'BEGIN {
 print "IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL"
 v1=v2=v3=v4=v5=v6=v7=v8=v9=v10=v11=v12=v13=v14=v15=v16=""; }
  /IMSIS=/ { split($0,arr,"="); v1=arr[2]; gsub(";","",v1); }
  NF==4 && !/IMSIS/ { v2=$2; v3=$3; split($4,arr,"-"); v4=arr[2]; }
  NF==1 && /BO/ { if($0 ~ /-/) { split($0,arr,"-"); v5=arr[2]; } else v5=1; }
  NF==1 && /PLMN/ { if($0 ~ /-/) { split($0,arr,"-"); v6=arr[2]; } else v5=1; }
  NF==1 && /MAPVER/ { if($0 ~ /-/) { split($0,arr,"-"); v7=arr[2]; } else v7=1; }
  NF==1 && /NRRG/ { if($0 ~ /-/) { split($0,arr,"-"); v8=arr[2]; } else v8=1; }
  NF==1 && /CBA/ { if($0 ~ /-/) { split($0,arr,"-"); v9=arr[2]; } else v9=1; }
  NF==1 && /CBAZ/ { if($0 ~ /-/) { split($0,arr,"-"); v10=arr[2]; } else v10=1; }
  NF==1 && /CAMEL/ { if($0 ~ /-/) { split($0,arr,"-"); v11=arr[2]; } else v11=1; }
  NF==1 && /INOPER/ { if($0 ~ /-/) { split($0,arr,"-"); v12=arr[2]; } else v12=1; }
  NF==1 && /OWNMS/ { if($0 ~ /-/) { split($0,arr,"-"); v13=arr[2]; } else v13=1; }
  NF==1 && /NATMS/ { if($0 ~ /-/) { split($0,arr,"-"); v14=arr[2]; } else v14=1; }
  NF==1 && /ERIS/ { if($0 ~ /-/) { split($0,arr,"-"); v15=arr[2]; } else v15=1; }
  NF==1 && /STALL/ { if($0 ~ /-/) { split($0,arr,"-"); v16=arr[2]; } else v16=1; }
  NF==0 {
   if(v2=="")
      printf ("%s,%s\n",v1,"UNIDENTIFIED");
   else printf ("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16);
  v2=v3=v4=v5=v6=v7=v8=v9=v10=v11=v12=v13=v14=v15=v16="";
}' filename | awk '!A[$0]++'

# 5  
Old 12-28-2012
Hello bipinajith, thank you again! It seems is closed, but is showing 942 in all lines and should appear the different series that begin with 942 instead. The current output is :
Code:
 IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL 942,6-33313,4,150,49,0,2,0,22,22,3,,,,, 942,6-889134,4,150,49,0,2,0,22,22,,,,,, 942,5-777714,4,150,49,0,2,0,50,50,0,,,,,

But the desired output is:
Code:
 IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL 942111,6-33313,4,150,49,0,2,0,22,22,3,,,,, 942123,6-889134,4,150,49,0,2,0,22,22,,,,,, 94201,5-777714,4,150,49,0,2,0,50,50,0,,,,,

Thanks in advance again. PS: Please moderator, fix the text within [code] tags, I don't now why is showing messy. Thanks.
# 6  
Old 12-28-2012
try:
Code:
awk '
BEGIN {
  s="IMSIS,M,NA,OBA,BO,PLMN,MAPVER,NRRG,CBA,CBAZ,CAMEL,INOPER,OWNMS,NATMS,ERIS,STALL";
  k=split(s,h,","); print s;
}
$1 == "END" && c==0 {print v ",UNIDENTIFIED"; delete a}
!$1 && c==1 {if (a[h[1]]) for (i=1; i<=k; i++) printf  (i<k) ? a[h[i]]",":a[h[i]]"\n"; delete a}
/<..*;/ {++n; c=0; j=split($0,o,"[:=;]"); w=o[j-2]; v=o[j-1]}
!$1 && c==0 {next}
$1 && c==1 {
  if (NF>1) for (i=1; i<=NF-1; i++) a[h[i]]=$i;
  f=v=$NF; x=sub("[-].*","", f); x=sub(".*[-]","", v);
  if (x) {a[f]=v} else {a[f]=1};
}
$1 == w {c=1}
' infile

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 12-28-2012
rdrtx1 Indeed genius use of awk arrays!!! Thank you very much for this useful post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Can I run repair on lot of blocks with single command ?

Hi, I have Solaris-10 OS on T5220. Both local disks were mirrored under SVM. Somehow when one disk gone bad (c0t1d0), other disk (c0t0d0) also got lot of bad block. We have readable data only on c0t0d0, but as soon as server comes after, it hangs when I run few commands because of read errors,... (1 Reply)
Discussion started by: solaris_1977
1 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

sed - filter blocks between single delimiters matching a pattern

Hi! I have a file with the following format:CDR ... MSISDN=111 ... CDR ... MSISDN=xxx ... CDR ... MSISDN=xxx ... CDR ... MSISDN=111 (2 Replies)
Discussion started by: Flavius
2 Replies

4. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

5. Shell Programming and Scripting

Row blocks to column blocks

Hello, Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row: S02 Length Per S02 7043 3.864 S02 54477 29.89 S02 104841 57.52 S03 Length Per S03 1150 0.835 S03 1321 0.96 S03 ... (9 Replies)
Discussion started by: yifangt
9 Replies

6. Shell Programming and Scripting

Show distinct values of a key from a single line

Hi All, I am newbie to linux. Can somebody help me with following requirement. I have one huge line. I have to find out particular key/value pair to see the distinct value of that key. Portion of the String:- <?xml version="1.1" encoding="UTF-8"?> <Data><Val Ti="1342750845538" Du="0"... (5 Replies)
Discussion started by: kmajumder
5 Replies

7. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

8. Shell Programming and Scripting

How to map the values of an array in perl?

Hi, I have 2 arrays: @names=qw(amith veena chaitra); @files=qw(file.txt file1.txt file3.txt); There is one to one relationship between names and files. There needs to be mapping created between names and files. The output should be like this: amith --> file.txt veena --->... (3 Replies)
Discussion started by: vanitham
3 Replies

9. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

10. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies
Login or Register to Ask a Question