Transposing a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transposing a file
# 1  
Old 12-07-2011
Transposing a file

Hi Guys,

I have file containing this kind of format below:
Code:
======== MOBILITY EVENT (G): ATTACH REJECT =========
Time      : <date_time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach type>
IMSI      : <imsi>
PTMSI     : <ptmsi>
RA New    : <ra new>
RA Old    : <ra old>
Cell ID   : <cell id>
HLR addr  : <hlr addr>


======== MOBILITY EVENT (W): ATTACH REJECT =========
Time      : <date_time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach type>
IMSI      : <imsi>
PTMSI     : <ptmsi>
RA New    : <ra new>
RA Old    : <ra old>
Cell ID   : <cell id>
HLR addr  : <hlr addr>


======== MOBILITY EVENT (W): ATTACH REJECT =========
Time      : <date_time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach type>
IMSI      : <imsi>
PTMSI     : <ptmsi>
RA New    : <ra new>
RA Old    : <ra old>
Cell ID   : <cell id>
HLR addr  : <hlr addr>

Each file contains maybe 2000-3000 file group (say for example, I have 3 file groups above).

Now, I want to create a table wherein it will output something like this:
Code:
Mobility Event          Time              Node        GMM Cause      Details         ....
        G                  <date_time>      <node>          <code>          <details>      ....
        W                 <date_time>      <node>          <code>          <details>      ....
        W                 <date_time>      <node>          <code>          <details>      ....


Can somebody help me please, pardon I'm just a beginner in scripting. You could either suggest Shell or Perl Programming on how to. Please Smilie


Best Regards,
Raymond

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-07-2011 at 05:55 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 12-07-2011
Try this

Code:
#!/usr/bin/perl

my $c=0;
my %r=();
while(<>) {
                if(/MOBILITY EVENT \(([A-Z])\)/) {
                        $c++;
                        $r{$c}=$1;
                }
                if(/^\s*(.*?)\s*:\s*(.*)\s*$/) {
                        $r{$c}{$1} = $2;
                }
}

printf("%-15s %-15s %-15s %-15s %-15s \n", 'Mobility Event', 'Time', 'Node', 'GMM Cause', 'Details');

foreach my $k (sort keys %r) {
        printf("%-15s %-15s %-15s %-15s %-15s\n", $r{$k}, $r{$k}{'Time'}, $r{$k}{'Node'}, $r{$k}{'GMM Cause'}, $r{$k}{'Details'});
}

Code:
$ cat data.txt
======== MOBILITY EVENT (G): ATTACH REJECT =========
Time      : <date_time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach type>
IMSI      : <imsi>
PTMSI     : <ptmsi>
RA New    : <ra new>
RA Old    : <ra old>
Cell ID   : <cell id>
HLR addr  : <hlr addr>


======== MOBILITY EVENT (W): ATTACH REJECT =========
Time      : <date_time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach type>
IMSI      : <imsi>
PTMSI     : <ptmsi>
RA New    : <ra new>
RA Old    : <ra old>
Cell ID   : <cell id>
HLR addr  : <hlr addr>


======== MOBILITY EVENT (W): ATTACH REJECT =========
Time      : <date_time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach type>
IMSI      : <imsi>
PTMSI     : <ptmsi>
RA New    : <ra new>
RA Old    : <ra old>
Cell ID   : <cell id>
HLR addr  : <hlr addr>
$ perl parse.pl < data.txt
Mobility Event  Time            Node            GMM Cause       Details
G               <date_time>     <node>          <code>          <details>
W               <date_time>     <node>          <code>          <details>
W               <date_time>     <node>          <code>          <details>

# 3  
Old 12-07-2011
thanks Mr. Bean!!! You're great man! I hope you could explain me also the script, Smilie Maybe per line would do. Thanks again Man!


Br,
Raymond
# 4  
Old 12-07-2011
Code:
# awk -F' : ' 'BEGIN{f=5;printf "%s%"f"c","Mobility Event"," "};NR==1{s=gensub(/.*\((.*)\).*/,"\\1",$1)}
NR>1{a[++x]=$2;for(i=1;i<NF;i++)printf "%"f"s%"f"c",$i," "}
END{printf "\n%"f"s",s;a[$2]++;for(i=0;i<NR;i++) {printf "%"f-3"c%"f+3"s"," ",a[i]}}' inputfile

regards
ygemici
# 5  
Old 12-07-2011
Hi Mr.bean,

I've tried your script and Yes it worked, but I have some problems here, why is that it outputs only one IMSI, see example below:
Code:
======== MOBILITY EVENT (W): ATTACH REJECT =========
Time      : <date time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach>
IMSI      : <imsi_01>
PTMSI     : <ptmsi>
RA New    : <ra_new>
RA Old    : <ra_old>
HLR addr  : <hlr_addr>


======== MOBILITY EVENT (G): ATTACH REJECT =========
Time      : <date time>
Node      : <node>
GMM Cause : <code>
Details   : <details>
Attach    : <attach>
IMSI      : <imsi_02>
PTMSI     : <ptmsi>
RA New    : <ra_new>
RA Old    : <ra_old>
HLR addr  : <hlr_addr>


Output:
Code:
Mobility Event    Time            Node        GMM Cause        Details        Attach        IMSI
W                     <date time>   <node>         <code>            <details>      <attach>    imsi01
G                      <date time>   <node>         <code>            <details>      <attach>    imsi01
W                     <date time>   <node>          <code>            <details>      <attach>    imsi01
G                      <date time>   <node>          <code>            <details>      <attach>    imsi01

IMSI data is the same in all lines, but as per checking the file that contains the actual data, IMSI is unique and different from each other.

Note: IMSI = 15digit_nos


Please advise Smilie


Br,
Rymnd

Last edited by Franklin52; 12-07-2011 at 09:53 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 12-07-2011
Bug

Tested, this should do it.

Code:
#!/usr/bin/perl
use strict;

my $c=0;
my %r=();
while(<>) {
                if(/MOBILITY EVENT \(([A-Z])\)/) {
                        $c++;
                        $r{$c}{$1} = {};
                        $r{$c}{'Event'} = $1;
                        next;
                }
                if(/^\s*(.*?)\s*:\s*(.*)\s*$/) {
                        $r{$c}{$1} = $2;
                }
}

printf("%-15s %-15s %-15s %-15s %-15s %-15s\n", 'Mobility Event', 'Time', 'Node', 'GMM Cause', 'Details', 'IMSI');

foreach my $k (sort keys %r) {
        printf("%-15s %-15s %-15s %-15s %-15s %-15s\n", $r{$k}{'Event'}, $r{$k}{'Time'}, $r{$k}{'Node'}, $r{$k}{'GMM Cause'}, $r{$k}{'Details'}, $r{$k}{'IMSI'});
}

# 7  
Old 12-08-2011
Hi Mr.bean, great thanks! I hope you could explain to me even these few lines below for me to understand also the logic of the code and in the future I could also share and help others. You can just put comments per line.

my $c=0; my %r=(); while(<>) { if(/MOBILITY EVENT \(([A-Z])\)/) { $c++; $r{$c}{$1} = {}; $r{$c}{'Event'} = $1; next; } if(/^\s*(.*?)\s*:\s*(.*)\s*$/) { $r{$c}{$1} = $2; } }

Last edited by vbe; 12-08-2011 at 05:19 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Transposing a huge space delimited file

Hi, How do I transpose a huge space delimited file with more than 2 million columns and about 100 rows? Thanks! (10 Replies)
Discussion started by: evelibertine
10 Replies

2. Shell Programming and Scripting

Transposing lines in a csv file with sed

Hi I have a large csv file with lines like below Date,Status,orig,dest,in,out,when,where 2012-01-01 00:30:37,I,48,56,23,98,83,34 2012-06-17 15:00:38,I,72,43,12,65,34,12I will really appreciate if someone can help with a sed script to transpose this to 2012-01-01 00:30:37,I,orig,48... (5 Replies)
Discussion started by: kaf3773
5 Replies

3. Shell Programming and Scripting

transposing square matrixs or blocks in a big file

Hi I do have a big file of the following format a b c d e f g 2 3 5 6 6 6 7 3 4 5 6 7 9 0 4 5 7 8 9 9 0 1 2 4 5 6 7 8 3 5 6 7 2 3 4 5 6 7 4 3 2 4 5 4 5 6 3 5 5 r h i j k l m 2 3 4 5 6 7 8 4 5 7 8 9 9 0 3 5 6 7 2 3 4 2 3 5 6 6 6 7 5 5 7 8 9 2 3 1 2... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

4. Shell Programming and Scripting

Transposing a file

Hi All, I have a input file say FILEA. FILEA -------- empid1 sal1 location1 manager1 empid2 sal2 location2 manager2 empid3 sal3 location3 manager3 . . . (3 Replies)
Discussion started by: 46019
3 Replies

5. Shell Programming and Scripting

Transposing column to row, joining with another file, then sorting columns

Hello! I am very new to Linux and I do not know where to begin... I have a column with >64,000 elements (that are not in numberical order) like this: name 2 5 9 . . . 64,000 I would like to transpose this column into a row that will later become the header of a very large file... (2 Replies)
Discussion started by: doobedoo
2 Replies

6. Shell Programming and Scripting

Transposing a file

i have a file as: 1 2 3 4 5 i want output as : 1 2 3 4 5 can anybody help on this?? (14 Replies)
Discussion started by: vikas_kesarwani
14 Replies

7. UNIX Desktop Questions & Answers

More than transposing!

Hi everyone, I have a poblem like that: I have a file which includes data looks like: 0.65214 0.3597 1.0 0.65244 0.3502 1.0 0.65273 0.3553 1.0 0.65305 0.3544 1.0 0.65327 0.3505 1.0 0.65359 0.3516 1.0 0.65578 0.6464 1.0 0.65605 0.6453 1.0 0.65633 0.6437 1.0 0.65660 0.6488 1.0... (3 Replies)
Discussion started by: bulash
3 Replies

8. Shell Programming and Scripting

Another transposing issue

Hello I need to sort a file with data such as so it breaks on column 1 and all the data in column 2 is sorted into rows with a unique column 1: 1 5 1 6 1 7 2 3 2 4 3 7 3 0 3 9 So it comes out as: 1 5 6 7 2 3 4 3 7 0 9 I've tried many iterations of nawk but can't get it... (14 Replies)
Discussion started by: stevesmith
14 Replies

9. Shell Programming and Scripting

file transposing

Hello, Is there a way to transpose a file in shell scripting? For instance, from a1 a2 a3 a4 a5 a6 a7 .... b1 b2 b3 b4 b5 b6 b7 .... c1 c2 c3 c4 c5 c6 c7 .... d1 d2 d3 d4 d5 d6 d7 ... ... ... ... to a1 b1 c1 d1 .... a2 b2 c2 d2 .... a3 b3 c3 d3 .... a4 b3 c3 d4 .... ... ... (24 Replies)
Discussion started by: mskcc
24 Replies

10. Shell Programming and Scripting

transposing letters

Hi, I've written a shell function in bash that reads letters into an array, then outputs them in one column with: for n in "${array}"; do echo $n done I was wondering if anyone knew how i would transpose the letters that are output by the for loop. Right now my output is: aabbcc... (4 Replies)
Discussion started by: myscsa2004
4 Replies
Login or Register to Ask a Question