Transpose lines from individual blocks to unique lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose lines from individual blocks to unique lines
# 1  
Old 01-01-2013
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.

https://www.unix.com/shell-programmin...le-line-2.html

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

# 2  
Old 01-01-2013
You could try a quick adaptation of rdrtx1's code:
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[i]]",":a[h[i]]"\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[i]]",":a[h[i]]"\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]]=$i;
  f=v=$NF; x=sub("[-].*","", f); x=sub(".*[-]","", v);
  if (x) {a[f]=v} else {a[f]=1};
}
$1 == w {c=1}
' infile


Last edited by Scrutinizer; 01-01-2013 at 05:54 PM..
# 3  
Old 01-01-2013
Hello Scrutinizer,

That was all needed? jaja

Few changes, I thought was needed many more things!

Thank youu for your great help!!!.

Best regards
# 4  
Old 01-02-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comma separated values to individual lines

My OS : RHEL 6.7 I have a text file with comma separated values like below $ cat testString.txt 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', . . . . I want these values to appear like below 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', .... (4 Replies)
Discussion started by: kraljic
4 Replies

2. UNIX for Dummies Questions & Answers

Print unique lines without sort or unique

I would like to print unique lines without sort or unique. Unfortunately the server I am working on does not have sort or unique. I have not been able to contact the administrator of the server to ask him to add it for several weeks. (7 Replies)
Discussion started by: cokedude
7 Replies

3. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

4. Shell Programming and Scripting

Insert date/time in reoccurring blocks of lines

Hi folks, I have a little problem, that is kinda bugging me :( ... I have a logfile, that looks kinda "crippled" in the raw format ... So far, I've managed, to get it in a format like this: PRI_PTA01,2W,30,30,0,0,0,0,0,0,0,0,0,0,0,0... (5 Replies)
Discussion started by: whizzler
5 Replies

5. Shell Programming and Scripting

[uniq + awk?] How to remove duplicate blocks of lines in files?

Hello again, I am wanting to remove all duplicate blocks of XML code in a file. This is an example: input: <string-array name="threeItems"> <item>item1</item> <item>item2</item> <item>item3</item> </string-array> <string-array name="twoItems"> <item>item1</item> <item>item2</item>... (19 Replies)
Discussion started by: raidzero
19 Replies

6. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

7. Shell Programming and Scripting

Working with individual blocks of text using awk

Hi, I am working with CVS log data and have some data as follows. RCS file: /cvsroot/eclipse/org.eclipse.debug.core/core/org/eclipse/debug/core/IBreakpointListener.java,v head: 1.14 branch: locks: strict access list: keyword substitution: o total revisions: 15; selected... (3 Replies)
Discussion started by: sandeepk1611
3 Replies

8. Shell Programming and Scripting

Remove All Lines Between Two Unique Lines

Hi all! Im wondering if its possible to remove all lines between two lines. Im working with a document like this: data1 data2 <Remove> data3 data4 </Remove> data5 data6 I need it to end up like this if that possible: data1 data2 data5 data6 There are multiple instances of... (2 Replies)
Discussion started by: Grizzly
2 Replies

9. Shell Programming and Scripting

Delete Blank Lines Between DHCP Host Blocks

Hi All, I have a dhcpd.conf file that gets static hosts added and removed via a shell script. After sometime, there becomes huge gaps of space ( blank lines ) between each host block. I tried a couple of sed one-liners; but, I can't seem to get the output I'm looking for. Also, I would like... (4 Replies)
Discussion started by: cstovall
4 Replies

10. Shell Programming and Scripting

Delete blocks of lines from text file

Hello, Hello Firends, I have file like below. I want to remove selected blocks say abc,pqr,lst. how can i remove those blocks from file. zone abc { blah blah blah } zone xyz { blah blah blah } zone pqr { blah blah blah } (4 Replies)
Discussion started by: nrbhole
4 Replies
Login or Register to Ask a Question