Transposing a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transposing a file
# 8  
Old 12-08-2011
Hopefully someone could explain it better than me Smilie

Code:
my $c=0;        # set counter to 0
my %r=();        # initialize hash %r

while(<>) {
        # if line matches: 'MOBILITY EVENT ([Letter from A to Z])',
        # do anything between the curly braces
        if(/MOBILITY EVENT \(([A-Z])\)/) {
            $c++;                # increment counter by 1
            $r{$c}{'Event'} = $1;        # $1 would be the letter within the bracket: MOBILITY EVENT ([Letter from A to Z])
                            # we initialize a subhash of the %r using counter $c by setting the key and value pairs (e.g: Event => G)
                            #
                            # we now have the following:
                            # r = (
                            #    1 => {Event => G}
                            # )
                            #
            next;                # skip to the next line
        }

        # if line matches: Something : Something
        if(/^\s*(.*?)\s*:\s*(.*)\s*$/) {        
                            # we assume the text on the left of the colon is the KEY
                            # we assume the text on the right of the colon is the VALUE
            $r{$c}{$1} = $2;        # set the key $1 and value pair $2 to the subhash $c of the %r hash
                            # r = (
                            #    1 => { Event => G, Time => <datetime> }
                            # )
                            #
                            # then we go on to the next line and do the same
                            # until we hit the MOBILITY EVENT line, increment counter by 1 and
                            # the cycle continues until the end of line of the file
        }
}

# print out the headers and specify the column width to 15
printf("%-15s %-15s %-15s %-15s %-15s %-15s\n", 'Mobility Event', 'Time', 'Node', 'GMM Cause', 'Details', 'IMSI');


# foreach record in hash %r
foreach my $k (sort keys %r) {
    
    # print out the record values
    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'});

}


Last edited by MR.bean; 12-08-2011 at 03:44 AM..
This User Gave Thanks to MR.bean For This Post:
# 9  
Old 12-08-2011
thanks again Mr.bean! I think I need a Perl 101 Tutorial Smilie
hope you could help me again in the future.

God bless!
rymnd_1234

---------- Post updated at 08:05 AM ---------- Previous update was at 03:04 AM ----------

Mr.bean,

I can now somehow understand the code, however I'm thinking this since a while ago:

if(/MOBILITY EVENT \(([A-Z])\)/) {
$c++;
$r{$c}{'Event'} = $1; #how did the code know that $1 = letter of mobility event
next;
}
if(/^\s*(.*?)\s*:\s*(.*)\s*$/) {
$r{$c}{$1} = $2; #same here, how did the system know that $1 = key and $2 = value, I mean how can the code interpret that value $1 = letter of the Mobility event?


Pls advise... thanks,
rymnd

---------- Post updated at 08:05 AM ---------- Previous update was at 08:05 AM ----------

Mr.bean,

I can now somehow understand the code, however I'm thinking this since a while ago:

if(/MOBILITY EVENT \(([A-Z])\)/) {
$c++;
$r{$c}{'Event'} = $1; #how did the code know that $1 = letter of mobility event
next;
}
if(/^\s*(.*?)\s*:\s*(.*)\s*$/) {
$r{$c}{$1} = $2; #same here, how did the system know that $1 = key and $2 = value, I mean how can the code interpret that value $1 = letter of the Mobility event?


Pls advise... thanks,
rymnd
# 10  
Old 12-09-2011
Quote:
Originally Posted by rymnd_12345
thanks again Mr.bean! I think I need a Perl 101 Tutorial Smilie
hope you could help me again in the future.

God bless!
rymnd_1234

.........................
..................................................
...........................................................................
rymnd
perl scripts have a few problems maybe i'm wrong in somewhere but seems incorrect..anyway for example our file is below//
Code:
# cat infile





======== MOBILITY EVENT (W): ATTACH REJECT =========
Time      : <date time_01>
Node      : <node_01>
GMM Cause : <code_01>
Details   : <details_01>
Attach    : <attach_01>
IMSI      : <imsi_01>
PTMSI     : <ptmsi_01>
RA New    : <ra_new_01>
RA Old    : <ra_old_01>
HLR addr  : <hlr_addr_01>









======== MOBILITY EVENT (G): ATTACH REJECT =========
Time      : <date time_02>
Node      : <node_02>
GMM Cause : <code_02>
Details   : <details_02>
Attach    : <attach_02>
IMSI      : <imsi_02>
PTMSI     : <ptmsi_02>
RA New    : <ra_new_02>
RA Old    : <ra_old_02>
HLR addr  : <hlr_addr_02>



======== MOBILITY EVENT (Y): ATTACH REJECT =========
Time      : <date time_03>
Node      : <node_03>
GMM Cause : <code_03>
Details   : <details_03>
Attach    : <attach_03>
IMSI      : <imsi_03>
PTMSI     : <ptmsi_03>
RA New    : <ra_new_03>
RA Old    : <ra_old_03>
HLR addr  : <hlr_addr_03>

======== MOBILITY EVENT (D): ATTACH REJECT =========
Time      : <date time_04>
Node      : <node_04>
GMM Cause : <code_04>
Details   : <details_04>
Attach    : <attach_04>
IMSI      : <imsi_04>
PTMSI     : <ptmsi_04>
RA New    : <ra_new_04>
RA Old    : <ra_old_04>
HLR addr  : <hlr_addr_04>

first code
Code:
# cat y.pl
#!/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'});
}

Code:
# ./y.pl infile
Mobility Event  Time            Node            GMM Cause       Details         IMSI
W               <date time_01>  <node_01>       <code_01>       <details_01>    <imsi_01>
G               <date time_02>  <node_02>       <code_02>       <details_02>    <imsi_02>
Y               <date time_03>  <node_03>       <code_03>       <details_03>    <imsi_03>
L               <date time_04>  <node_04>       <code_04>       <details_04>    <imsi_04>
Q               <date time_05>  <node_05>       <code_05>       <details_05>    <imsi_05>

second perl code
Code:
# cat y2.pl
my $c=0;        # set counter to 0
my %r=();        # initialize hash %r

while(<>) {
        # if line matches: 'MOBILITY EVENT ([Letter from A to Z])',
        # do anything between the curly braces
        if(/MOBILITY EVENT \(([A-Z])\)/) {
            $c++;                # increment counter by 1
            $r{$c}{'Event'} = $1;        # $1 would be the letter within the bracket: MOBILITY EVENT ([Letter from A to Z])
                            # we initialize a subhash of the %r using counter $c by setting the key and value pairs (e.g: Event => G)
                            #
                            # we now have the following:
                            # r = (
                            #    1 => {Event => G}
                            # )
                            #
            next;                # skip to the next line
        }

        # if line matches: Something : Something
        if(/^\s*(.*?)\s*:\s*(.*)\s*$/) {        
                            # we assume the text on the left of the colon is the KEY
                            # we assume the text on the right of the colon is the VALUE
            $r{$c}{$1} = $2;        # set the key $1 and value pair $2 to the subhash $c of the %r hash
                            # r = (
                            #    1 => { Event => G, Time => <datetime> }
                            # )
                            #
                            # then we go on to the next line and do the same
                            # until we hit the MOBILITY EVENT line, increment counter by 1 and
                            # the cycle continues until the end of line of the file
        }
}

# print out the headers and specify the column width to 15
printf("%-15s %-15s %-15s %-15s %-15s %-15s\n", 'Mobility Event', 'Time', 'Node', 'GMM Cause', 'Details', 'IMSI');


# foreach record in hash %r
foreach my $k (sort keys %r) {
    
    # print out the record values
    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'});

}

Code:
# ./y2.pl infile
./y2.pl: line 1: my: command not found
./y2.pl: line 2: syntax error near unexpected token `('
./y2.pl: line 2: `my %r=();        # initialize hash %r'

you can try justdoit awk code Smilie [ i have tried a few hours for code :P Smilie]
Code:
# awk -F':' 'BEGIN{f=2;printf "%"f"s%c"," Mobility Event  "," "}{
if(NR==1&&/^$/)while(getline==/^$/){};{ss=gensub(/.*\((.*)\).*/,"\\1",$1);};}{
while(getline){if(!/^$/){gsub(/ */,"",$2);a[x++]=$2;b[xx++]=$1;;rx=x}
else{while(getline==/^$/){};
if(cc<1){for(i=0;i<xx;i++){if(i==xx/2)printf "%"f*4"c%"f+f"s"," ",b[i];
if(i==xx/2-1)printf "%"f*5"c%"f+f"s"," ",b[i];
if(i==xx-1)printf "%"f*4"c%"f+f"s"," ",b[i];
if(i!=xx/2&&i!=xx/2-1&&i!=xx-1)printf "%"f"s%"f+f"s"," ",b[i];};printf "\n%"f*4"c%"f"s"," ",ss;;};cc++;{
for(i=0;i<rx;i++){printf "%"f+f"c%"f+3"s"," ",a[i];};s=gensub(/.*\((.*)\).*/,"\\1",$1);
printf "\n%"f+f+f+f"c%"f"s"," ",s};x=0}}}' infile
 Mobility Event     Time        Node        GMM Cause   Details             Attach            IMSI        PTMSI       RA New      RA Old            HLR addr
         W    <datetime_01>    <node_01>    <code_01>    <details_01>    <attach_01>    <imsi_01>    <ptmsi_01>    <ra_new_01>    <ra_old_01>    <hlr_addr_01>
         G    <datetime_02>    <node_02>    <code_02>    <details_02>    <attach_02>    <imsi_02>    <ptmsi_02>    <ra_new_02>    <ra_old_02>    <hlr_addr_02>
         Y    <datetime_03>    <node_03>    <code_03>    <details_03>    <attach_03>    <imsi_03>    <ptmsi_03>    <ra_new_03>    <ra_old_03>    <hlr_addr_03>
         D    <datetime_04>    <node_04>    <code_04>    <details_04>    <attach_04>    <imsi_04>    <ptmsi_04>    <ra_new_04>    <ra_old_04>    <hlr_addr_04>

regards
ygemici
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