Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing file - Regex question - variable amount of records per row 1

Status
Not open for further replies.

dmazzini

Programmer
Jan 20, 2004
480
US
Hi guys

I am parsing a file where the records are variable. e.g
One row may have different amount of records. Records are separated by spaces, so if record dont exist I want to put "NA" or leave blank since I will create a output file comma separated value:

e.g record1,record,record3....,recordn

I have post code and input data and desired output, so you could have a look at it. First regex works fine, I have problems with second regex. I am not familiar using ? character...I believe it is the way to do it.

So here we go....

Code:
Input
-----

          NE ID            TARGETID    INTERNAL ID                                                              DN                  NE NAME
   FEATURE CODE                             FEATURE NAME     STATE     BASIC  LICENSED       MAX                      TIME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------


   WBTS-1695         L6070407474         824477                                     PLMN-PLMN/RNC-115/WBTS-1695                  ToyCell
            306                     HSDPA 16 QAM Support       OFF                                19-03-2008 17:42:42.7601
            310        Flexi WCDMA BTS Nokia MHA Support       OFF                                19-03-2008 17:42:42.7704
            598                                                OFF         2         0         4  19-03-2008 17:42:42.7729
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 17:42:42.7628
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 17:42:42.7655
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 17:42:42.7680
            616                                IMA (FTM)        ON                 YES            19-03-2008 17:42:42.7754
            
            
Desired Output 
-------------

NE ID,TARGETID,INTERNAL ID,DN,NE NAME,FEATURE CODE,FEATURE NAME,STATE,BASIC,LICENSED,MAX,TIME
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,306,HSDPA 16 QAM Support,OFF,,,,19-03-2008 17:42:42.7601
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,310,Flexi WCDMA BTS Nokia MHA Support ,OFF,,,,19-03-2008 17:42:42.7704
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,598,,OFF,2,0,4,19-03-2008 17:42:42.7729
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,307,HSDPA PFRP Scheduler,ON,,YES,,19-03-2008 17:42:42.7628 
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,308,Licence Based BTS channel capacity,ON,32,160,192,19-03-2008 17:42:42.7655
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,309,Antenna Line Supervision,ON,,YES,,19-03-2008 17:42:42.7680
1695,L6070407474,824477,PLMN-PLMN/RNC-115/WBTS-1695,ToyCell,616,IMA (FTM),ON,,YES,,19-03-2008 17:42:42.7754
Code:
#!/usr/bin/perl

use strict;

my ($line);
my ($ne_class,$ne_id,$target_id,$internal_id,$dn,$ne_name,$rnc_id,$wbts_id);
my ($feature_code,$feature_name,$state,$basic,$licensed,$max,$timestamp);


while (my $intext = <DATA>) { 
	    chomp $intext;		
	    $intext=trim($intext);
	    
	    if ($intext =~ /WBTS\-\d+\s*(\S+)\s*(\d+)\s*(PLMN\-PLMN\/RNC\-(\d+)\/WBTS\-(\d+))\s*(\S+)/) { # This regex works ok    
         ($target_id,$internal_id,$dn,$rnc_id,$wbts_id,$ne_name)=($1,$2,$3,$4,$5,$6);
          print  "TARGET:$target_id,
                  INTERNAL ID: $internal_id,
                  DN: $dn,
                  RNC: $rnc_id,
                  WBTS: $wbts_id,
                  NE:$ne_name\n";        
      }
      
    
      if ($intext =~ /^(\d+)\s*(.+)?\s*(OFF|ON)\s*(\d+)?\s*(.+)?\s*(.+)?\s*(.+)?/) {  # This regex does not work well.       
         	$feature_code ="NA"unless defined($1); 
         	$feature_code =$1 if defined($1);
         	
         	$feature_name ="NA"unless defined($2); 
         	$feature_name =$2 if defined($2); 
         	
         	$state ="NA"unless defined($3); 
         	$state =$3 if defined($3); 
         	
          $basic ="NA"unless defined($4); 
         	$basic =$4 if defined($4); 
         	
         	$licensed ="NA"unless defined($5); 
         	$licensed =$5 if defined($5); 
         	
         	$max ="NA"unless defined($6); 
         	$max =$6 if defined($6);
         	
         	$timestamp ="NA"unless defined($7); 
         	$timestamp =$7 if defined($7); 
         	
         	$feature_code=trim($feature_code);
          $feature_name=trim($feature_name);          
         	$state=trim($state);    
         	$basic=trim($basic); 
         	$licensed= trim($licensed);  
         	$max= trim($max);
         	$timestamp=trim($timestamp); 	         	
         	
         	print  "FEATURE CODE:$feature_code,
         	        FEAT NAME:$feature_name,
         	        STATE:$state,	
         	        BASIC:$basic,	
         	        LIC:$licensed,
         	        MAX:$max,
         	        TIME:$timestamp\n";
        
      }
      
      
} 


# #################################################################
# Sub trim
# #################################################################


sub trim {
    my @out = @_;
    for (@out) {
        s/^\s+//;          # trim left
        s/\s+$//;          # trim right
    }
    return @out == 1 
              ? $out[0]   # only one to return
              : @out;     # or many
}



__DATA__


Features of NE Report 29.05.2008 18:07:20 EDT

          NE ID            TARGETID    INTERNAL ID                                                              DN                  NE NAME
   FEATURE CODE                             FEATURE NAME     STATE     BASIC  LICENSED       MAX                      TIME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
      WBTS-1695         L6070407474         824477                                     PLMN-PLMN/RNC-115/WBTS-1695                  ToyCell
            306                     HSDPA 16 QAM Support       OFF                                19-03-2008 17:42:42.7601
            310        Flexi WCDMA BTS Nokia MHA Support       OFF                                19-03-2008 17:42:42.7704
            598                                                OFF         2         0         4  19-03-2008 17:42:42.7729
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 17:42:42.7628
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 17:42:42.7655
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 17:42:42.7680
            616                                IMA (FTM)        ON                 YES            19-03-2008 17:42:42.7754
      WBTS-5177         L6075014217         868630                                     PLMN-PLMN/RNC-115/WBTS-5177                U6MD1177A
            434                                                OFF         0                   3  19-03-2008 18:01:54.3435
            598                                                OFF         2         0         4  19-03-2008 18:01:54.3495
            618                                                OFF         0         0         4  19-03-2008 18:01:54.3550
            306                     HSDPA 16 QAM Support        ON                 YES            19-03-2008 18:01:54.3290
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 18:01:54.3324
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 18:01:54.3352
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 18:01:54.3378
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  19-03-2008 18:01:54.3406
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  19-03-2008 18:01:54.3463
            616                                IMA (FTM)        ON         0         0         1  19-03-2008 18:01:54.3523
      WBTS-5079         L6063909786         871738                                     PLMN-PLMN/RNC-115/WBTS-5079                U6MD1079K
            434                                                OFF         0                   3  19-03-2008 18:01:53.8934
            598                                                OFF         2         0         4  19-03-2008 18:01:53.8987
            618                                                OFF         0         0         4  19-03-2008 18:01:53.9044
            306                     HSDPA 16 QAM Support        ON                 YES            19-03-2008 18:01:53.8797
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 18:01:53.8827
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 18:01:53.8853
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 18:01:53.8880
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            19-03-2008 18:01:53.8908
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  19-03-2008 18:01:53.8960
            616                                IMA (FTM)        ON         0         0         1  19-03-2008 18:01:53.9016
      WBTS-2192         L6074935867         871745                                     PLMN-PLMN/RNC-215/WBTS-2192                U6FB1192B
            434                                                OFF         0                   3  19-03-2008 18:01:53.1370
            598                                                OFF         2         0         4  19-03-2008 18:01:53.1422
            618                                                OFF         0         0         3  19-03-2008 18:01:53.1475
            306                     HSDPA 16 QAM Support        ON                 YES            19-03-2008 18:01:53.1226
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 18:01:53.1256
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 18:01:53.1285
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 18:01:53.1315
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            19-03-2008 18:01:53.1342
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         3  19-03-2008 18:01:53.1396
            616                                IMA (FTM)        ON         0         0         1  19-03-2008 18:01:53.1449
      WBTS-2148         L6074707455         871750                                     PLMN-PLMN/RNC-215/WBTS-2148                U6FB1148C
            306                     HSDPA 16 QAM Support        ON                 YES            27-05-2008 03:42:14.0457
            307                     HSDPA PFRP Scheduler        ON                 YES            27-05-2008 03:42:14.0483
            308       Licence Based BTS channel capacity        ON        32       160       192  27-05-2008 03:42:14.0508
            309                 Antenna Line Supervision        ON                 YES            27-05-2008 03:42:14.0533
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            27-05-2008 03:42:14.0559
            434                                                 ON         0                   3  27-05-2008 03:42:14.0583
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  27-05-2008 03:42:14.0609
            598                                                 ON         2         0         4  27-05-2008 03:42:14.0634
            616                                IMA (FTM)        ON         0         0         1  27-05-2008 03:42:14.0661
            618                                                 ON         0         0         4  27-05-2008 03:42:14.0686
      WBTS-2052         L6074934002         872012                                     PLMN-PLMN/RNC-115/WBTS-2052                U6FB1052H
            434                                                OFF         0                   3  16-04-2008 12:04:17.5588
            598                                                OFF         2         0         4  16-04-2008 12:04:17.5638
            618                                                OFF         0         0         4  16-04-2008 12:04:17.5688
            306                     HSDPA 16 QAM Support        ON                 YES            16-04-2008 12:04:17.5463
            307                     HSDPA PFRP Scheduler        ON                 YES            16-04-2008 12:04:17.5489
            308       Licence Based BTS channel capacity        ON        32       160       192  16-04-2008 12:04:17.5514
            309                 Antenna Line Supervision        ON                 YES            16-04-2008 12:04:17.5538
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            16-04-2008 12:04:17.5564
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  16-04-2008 12:04:17.5613
            616                                IMA (FTM)        ON         0         0         1  16-04-2008 12:04:17.5663
      WBTS-2111         L6074419321         872020                                     PLMN-PLMN/RNC-115/WBTS-2111                U6FB1111A
            306                     HSDPA 16 QAM Support        ON                 YES            06-05-2008 17:28:51.1110
            307                     HSDPA PFRP Scheduler        ON                 YES            06-05-2008 17:28:51.1140
            308       Licence Based BTS channel capacity        ON        32       160       192  06-05-2008 17:28:51.1168
            309                 Antenna Line Supervision        ON                 YES            06-05-2008 17:28:51.1194
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            06-05-2008 17:28:51.1219
            434                                                 ON         0                   3  06-05-2008 17:28:51.1243
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  06-05-2008 17:28:51.1268
            598                                                 ON         2         0         4  06-05-2008 17:28:51.1292
            616                                IMA (FTM)        ON         0         0         1  06-05-2008 17:28:51.1321
            618                                                 ON         0         0         4  06-05-2008 17:28:51.1345
      WBTS-5121         L6064813309         872024                                     PLMN-PLMN/RNC-115/WBTS-5121                U6MD1121D
            434                                                OFF         0                   3  19-03-2008 18:01:54.0423
            598                                                OFF         2         0         4  19-03-2008 18:01:54.0478
            618                                                OFF         0         0         4  19-03-2008 18:01:54.0534
            306                     HSDPA 16 QAM Support        ON                 YES            19-03-2008 18:01:54.0274
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 18:01:54.0303
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 18:01:54.0333
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 18:01:54.0367
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  19-03-2008 18:01:54.0393
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  19-03-2008 18:01:54.0450
            616                                IMA (FTM)        ON         0         0         1  19-03-2008 18:01:54.0508
      WBTS-5126         L6064806084         872028                                     PLMN-PLMN/RNC-115/WBTS-5126                U6MD1126A
            434                                                OFF         0                   3  10-04-2008 15:35:34.9971
            598                                                OFF         2         0         4  10-04-2008 15:35:35.0021
            618                                                OFF         0         0         4  10-04-2008 15:35:35.0070
            306                     HSDPA 16 QAM Support        ON                 YES            10-04-2008 15:35:34.9843
            307                     HSDPA PFRP Scheduler        ON                 YES            10-04-2008 15:35:34.9868
            308       Licence Based BTS channel capacity        ON        32       160       192  10-04-2008 15:35:34.9893
            309                 Antenna Line Supervision        ON                 YES            10-04-2008 15:35:34.9921
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            10-04-2008 15:35:34.9947
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  10-04-2008 15:35:34.9996
            616                                IMA (FTM)        ON         0         0         1  10-04-2008 15:35:35.0046
      WBTS-2136         L6064705166         872032                                     PLMN-PLMN/RNC-115/WBTS-2136                U6FB1136D
            434                                                OFF         0                   3  19-03-2008 18:01:53.7377
            598                                                OFF         2         0         4  19-03-2008 18:01:53.7431
            618                                                OFF         0         0         4  19-03-2008 18:01:53.7487
            306                     HSDPA 16 QAM Support        ON                 YES            19-03-2008 18:01:53.7233
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 18:01:53.7262
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 18:01:53.7292
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 18:01:53.7321
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  19-03-2008 18:01:53.7349
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  19-03-2008 18:01:53.7402
            616                                IMA (FTM)        ON         0         0         1  19-03-2008 18:01:53.7460
      WBTS-5136         L6074709913         872036                                     PLMN-PLMN/RNC-115/WBTS-5136                U6MD1136A
            434                                                OFF         0                   3  19-03-2008 18:01:54.1220
            598                                                OFF         2         0         4  19-03-2008 18:01:54.1270
            618                                                OFF         0         0         4  19-03-2008 18:01:54.1319
            306                     HSDPA 16 QAM Support        ON                 YES            19-03-2008 18:01:54.1088
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 18:01:54.1115
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 18:01:54.1141
            309                 Antenna Line Supervision        ON                 YES            19-03-2008 18:01:54.1168
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  19-03-2008 18:01:54.1193
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  19-03-2008 18:01:54.1245
            616                                IMA (FTM)        ON         0         0         1  19-03-2008 18:01:54.1294
      WBTS-2059         L6064705138         872090                                     PLMN-PLMN/RNC-115/WBTS-2059                U6FB1059D
            306                     HSDPA 16 QAM Support        ON                 YES            24-04-2008 12:53:02.1580
            307                     HSDPA PFRP Scheduler        ON                 YES            24-04-2008 12:53:02.1607
            308       Licence Based BTS channel capacity        ON        32       160       192  24-04-2008 12:53:02.1632
            309                 Antenna Line Supervision        ON                 YES            24-04-2008 12:53:02.1657
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            24-04-2008 12:53:02.1686
            434                                                 ON         0                   3  24-04-2008 12:53:02.1717
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  24-04-2008 12:53:02.1743
            598                                                 ON         2         0         4  24-04-2008 12:53:02.1768
            616                                IMA (FTM)        ON         0         0         1  24-04-2008 12:53:02.1796
            618                                                 ON         0         0         4  24-04-2008 12:53:02.1823
      WBTS-6102         L6074926879         872091                                     PLMN-PLMN/RNC-115/WBTS-6102                U6MD2102D
            434                                                OFF         0                   3  02-04-2008 12:42:19.7297
            598                                                OFF         2         0         4  02-04-2008 12:42:19.7346
            618                                                OFF         0         0         3  02-04-2008 12:42:19.7395
            306                     HSDPA 16 QAM Support        ON                 YES            02-04-2008 12:42:19.7171
            307                     HSDPA PFRP Scheduler        ON                 YES            02-04-2008 12:42:19.7196
            308       Licence Based BTS channel capacity        ON        32       160       192  02-04-2008 12:42:19.7221
            309                 Antenna Line Supervision        ON                 YES            02-04-2008 12:42:19.7246
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            02-04-2008 12:42:19.7272
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         3  02-04-2008 12:42:19.7321
            616                                IMA (FTM)        ON         0         0         1  02-04-2008 12:42:19.7371
      WBTS-5390         L6074707454         872092                                     PLMN-PLMN/RNC-115/WBTS-5390                U6MD1390B
            434                                                OFF         0                   3  20-05-2008 03:37:37.3042
            598                                                OFF         2         0         4  20-05-2008 03:37:37.3092
            618                                                OFF         0         0         4  20-05-2008 03:37:37.3141
            306                     HSDPA 16 QAM Support        ON                 YES            20-05-2008 03:37:37.2917
            307                     HSDPA PFRP Scheduler        ON                 YES            20-05-2008 03:37:37.2942
            308       Licence Based BTS channel capacity        ON        32       160       192  20-05-2008 03:37:37.2968
            309                 Antenna Line Supervision        ON                 YES            20-05-2008 03:37:37.2993
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  20-05-2008 03:37:37.3017
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  20-05-2008 03:37:37.3067
            616                                IMA (FTM)        ON         0         0         1  20-05-2008 03:37:37.3116
      WBTS-5385         L6073629729         872093                                     PLMN-PLMN/RNC-115/WBTS-5385                U6MD1385B
            306                     HSDPA 16 QAM Support        ON                 YES            06-05-2008 18:04:42.7461
            307                     HSDPA PFRP Scheduler        ON                 YES            06-05-2008 18:04:42.7487
            308       Licence Based BTS channel capacity        ON        32       160       192  06-05-2008 18:04:42.7512
            309                 Antenna Line Supervision        ON                 YES            06-05-2008 18:04:42.7537
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  06-05-2008 18:04:42.7562
            434                                                 ON         0                   3  06-05-2008 18:04:42.7587
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  06-05-2008 18:04:42.7612
            598                                                 ON         2         0         4  06-05-2008 18:04:42.7637
            616                                IMA (FTM)        ON         0         0         1  06-05-2008 18:04:42.7664
            618                                                 ON         0         0         4  06-05-2008 18:04:42.7689
      WBTS-5384         L6064802305         872094                                     PLMN-PLMN/RNC-115/WBTS-5384                U6MD1384B
            306                     HSDPA 16 QAM Support        ON                 YES            08-05-2008 11:18:25.6816
            307                     HSDPA PFRP Scheduler        ON                 YES            08-05-2008 11:18:25.6845
            308       Licence Based BTS channel capacity        ON        32       160       192  08-05-2008 11:18:25.6871
            309                 Antenna Line Supervision        ON                 YES            08-05-2008 11:18:25.6896
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            08-05-2008 11:18:25.6925
            434                                                 ON         0                   3  08-05-2008 11:18:25.6954
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         4         4  08-05-2008 11:18:25.6983
            598                                                 ON         2         0         4  08-05-2008 11:18:25.7012
            616                                IMA (FTM)        ON         0         0         1  08-05-2008 11:18:25.7038
            618                                                 ON         0         0         4  08-05-2008 11:18:25.7062
      WBTS-5353         L6080204924         872095                                     PLMN-PLMN/RNC-115/WBTS-5353                U6MD1353A
            306                     HSDPA 16 QAM Support        ON                 YES            26-05-2008 20:53:06.1335
            307                     HSDPA PFRP Scheduler        ON                 YES            26-05-2008 20:53:06.1361
            308       Licence Based BTS channel capacity        ON        32       160       192  26-05-2008 20:53:06.1387
            309                 Antenna Line Supervision        ON                 YES            26-05-2008 20:53:06.1413
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  26-05-2008 20:53:06.1439
            434                                                 ON         0                   3  26-05-2008 20:53:06.1464
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  26-05-2008 20:53:06.1490
            598                                                 ON         2         0         4  26-05-2008 20:53:06.1519
            616                                IMA (FTM)        ON         0         0         1  26-05-2008 20:53:06.1544
            618                                                 ON         0         0         4  26-05-2008 20:53:06.1571
      WBTS-5258         L6064501465         872096                                     PLMN-PLMN/RNC-115/WBTS-5258                U6MD1258A
            306                     HSDPA 16 QAM Support        ON                 YES            16-04-2008 13:29:34.5275
            307                     HSDPA PFRP Scheduler        ON                 YES            16-04-2008 13:29:34.5301
            308       Licence Based BTS channel capacity        ON        32       160       192  16-04-2008 13:29:34.5327
            309                 Antenna Line Supervision        ON                 YES            16-04-2008 13:29:34.5354
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  16-04-2008 13:29:34.5378
            434                                                 ON         0                   3  16-04-2008 13:29:34.5403
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  16-04-2008 13:29:34.5428
            598                                                 ON         2         0         4  16-04-2008 13:29:34.5453
            616                                IMA (FTM)        ON         0         0         1  16-04-2008 13:29:34.5477
            618                                                 ON         0         0         4  16-04-2008 13:29:34.5502
      WBTS-5254         L6080122549         872097                                     PLMN-PLMN/RNC-115/WBTS-5254                U6MD1254A
            306                     HSDPA 16 QAM Support        ON                 YES            18-04-2008 10:21:39.2672
            307                     HSDPA PFRP Scheduler        ON                 YES            18-04-2008 10:21:39.2699
            308       Licence Based BTS channel capacity        ON        32       160       192  18-04-2008 10:21:39.2726
            309                 Antenna Line Supervision        ON                 YES            18-04-2008 10:21:39.2751
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            18-04-2008 10:21:39.2777
            434                                                 ON         0                   3  18-04-2008 10:21:39.2802
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         3  18-04-2008 10:21:39.2830
            598                                                 ON         2         0         4  18-04-2008 10:21:39.2855
            616                                IMA (FTM)        ON         0         0         1  18-04-2008 10:21:39.2880
            618                                                 ON         0         0         3  18-04-2008 10:21:39.2904
      WBTS-5214         L6080306984         872100                                     PLMN-PLMN/RNC-115/WBTS-5214                U6MD1214A
            306                     HSDPA 16 QAM Support        ON                 YES            27-05-2008 03:37:18.6690
            307                     HSDPA PFRP Scheduler        ON                 YES            27-05-2008 03:37:18.6715
            308       Licence Based BTS channel capacity        ON        32       160       192  27-05-2008 03:37:18.6741
            309                 Antenna Line Supervision        ON                 YES            27-05-2008 03:37:18.6766
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            27-05-2008 03:37:18.6791
            434                                                 ON         0                   3  27-05-2008 03:37:18.6819
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  27-05-2008 03:37:18.6845
            598                                                 ON         2         0         4  27-05-2008 03:37:18.6871
            616                                IMA (FTM)        ON         0         0         1  27-05-2008 03:37:18.6896
            618                                                 ON         0         0         4  27-05-2008 03:37:18.6921
      WBTS-5211         L6064806080         872101                                     PLMN-PLMN/RNC-115/WBTS-5211                U6MD1211C
            434                                                OFF         0                   3  02-04-2008 12:42:19.3650
            598                                                OFF         2         0         4  02-04-2008 12:42:19.3706
            618                                                OFF         0         0         4  02-04-2008 12:42:19.3759
            306                     HSDPA 16 QAM Support        ON                 YES            02-04-2008 12:42:19.3520
            307                     HSDPA PFRP Scheduler        ON                 YES            02-04-2008 12:42:19.3545
            308       Licence Based BTS channel capacity        ON        32       160       192  02-04-2008 12:42:19.3571
            309                 Antenna Line Supervision        ON                 YES            02-04-2008 12:42:19.3597
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  02-04-2008 12:42:19.3624
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  02-04-2008 12:42:19.3679
            616                                IMA (FTM)        ON         0         0         1  02-04-2008 12:42:19.3733
      WBTS-5194         L6074707451         872103                                     PLMN-PLMN/RNC-115/WBTS-5194                U6MD1194B
            434                                                OFF         0                   3  27-05-2008 01:00:45.8518
            598                                                OFF         2         0         4  27-05-2008 01:00:45.8575
            618                                                OFF         0         0         4  27-05-2008 01:00:45.8627
            306                     HSDPA 16 QAM Support        ON                 YES            27-05-2008 01:00:45.8377
            307                     HSDPA PFRP Scheduler        ON                 YES            27-05-2008 01:00:45.8403
            308       Licence Based BTS channel capacity        ON        32       320       192  27-05-2008 01:00:45.8428
            309                 Antenna Line Supervision        ON                 YES            27-05-2008 01:00:45.8454
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            27-05-2008 01:00:45.8490
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  27-05-2008 01:00:45.8544
            616                                IMA (FTM)        ON         0         0         1  27-05-2008 01:00:45.8601
      WBTS-5192         L6064813316         872104                                     PLMN-PLMN/RNC-115/WBTS-5192                U6MD1192A
            434                                                OFF         0                   3  02-04-2008 12:42:19.2250
            598                                                OFF         2         0         4  02-04-2008 12:42:19.2303
            618                                                OFF         0         0         4  02-04-2008 12:42:19.2352
            306                     HSDPA 16 QAM Support        ON                 YES            02-04-2008 12:42:19.2126
            307                     HSDPA PFRP Scheduler        ON                 YES            02-04-2008 12:42:19.2150
            308       Licence Based BTS channel capacity        ON        32       160       192  02-04-2008 12:42:19.2177
            309                 Antenna Line Supervision        ON                 YES            02-04-2008 12:42:19.2201
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            02-04-2008 12:42:19.2226
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  02-04-2008 12:42:19.2278
            616                                IMA (FTM)        ON         0         0         1  02-04-2008 12:42:19.2327
      WBTS-5187         L6064813270         872105                                     PLMN-PLMN/RNC-115/WBTS-5187                U6MD1187C
            434                                                OFF         0                   3  25-05-2008 15:44:24.2694
            598                                                OFF         2         0         4  25-05-2008 15:44:24.2745
            618                                                OFF         0         0         4  25-05-2008 15:44:24.2795
            306                     HSDPA 16 QAM Support        ON                 YES            25-05-2008 15:44:24.2566
            307                     HSDPA PFRP Scheduler        ON                 YES            25-05-2008 15:44:24.2592
            308       Licence Based BTS channel capacity        ON        32       160       192  25-05-2008 15:44:24.2617
            309                 Antenna Line Supervision        ON                 YES            25-05-2008 15:44:24.2644
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            25-05-2008 15:44:24.2669
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  25-05-2008 15:44:24.2719
            616                                IMA (FTM)        ON         0         0         1  25-05-2008 15:44:24.2770
      WBTS-5186         L6074934005         872106                                     PLMN-PLMN/RNC-115/WBTS-5186                U6MD1186A
            306                     HSDPA 16 QAM Support        ON                 YES            24-04-2008 12:22:48.5209
            307                     HSDPA PFRP Scheduler        ON                 YES            24-04-2008 12:22:48.5235
            308       Licence Based BTS channel capacity        ON        32       160       192  24-04-2008 12:22:48.5260
            309                 Antenna Line Supervision        ON                 YES            24-04-2008 12:22:48.5285
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            24-04-2008 12:22:48.5310
            434                                                 ON         0                   3  24-04-2008 12:22:48.5335
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  24-04-2008 12:22:48.5364
            598                                                 ON         2         0         4  24-04-2008 12:22:48.5390
            616                                IMA (FTM)        ON         0         0         1  24-04-2008 12:22:48.5416
            618                                                 ON         0         0         4  24-04-2008 12:22:48.5442
      WBTS-5184         L6064501445         872107                                     PLMN-PLMN/RNC-115/WBTS-5184                U6MD1184C
            306                     HSDPA 16 QAM Support        ON                 YES            24-04-2008 15:23:53.5426
            307                     HSDPA PFRP Scheduler        ON                 YES            24-04-2008 15:23:53.5455
            308       Licence Based BTS channel capacity        ON        32       160       192  24-04-2008 15:23:53.5480
            309                 Antenna Line Supervision        ON                 YES            24-04-2008 15:23:53.5505
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  24-04-2008 15:23:53.5530
            434                                                 ON         0                   3  24-04-2008 15:23:53.5555
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  24-04-2008 15:23:53.5581
            598                                                 ON         2         0         4  24-04-2008 15:23:53.5606
            616                                IMA (FTM)        ON         0         0         1  24-04-2008 15:23:53.5630
            618                                                 ON         0         0         4  24-04-2008 15:23:53.5656
      WBTS-5182         L6064806089         872108                                     PLMN-PLMN/RNC-115/WBTS-5182                U6MD1182D
            306                     HSDPA 16 QAM Support        ON                 YES            02-05-2008 12:55:23.8411
            307                     HSDPA PFRP Scheduler        ON                 YES            02-05-2008 12:55:23.8436
            308       Licence Based BTS channel capacity        ON        32       160       192  02-05-2008 12:55:23.8461
            309                 Antenna Line Supervision        ON                 YES            02-05-2008 12:55:23.8486
            310        Flexi WCDMA BTS Nokia MHA Support        ON         0         1         4  02-05-2008 12:55:23.8511
            434                                                 ON         0                   3  02-05-2008 12:55:23.8536
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  02-05-2008 12:55:23.8561
            598                                                 ON         2         0         4  02-05-2008 12:55:23.8587
            616                                IMA (FTM)        ON         0         0         1  02-05-2008 12:55:23.8612
            618                                                 ON         0         0         4  02-05-2008 12:55:23.8637
      WBTS-5173         L6075134231         872109                                     PLMN-PLMN/RNC-115/WBTS-5173                U6MD1173A
            306                     HSDPA 16 QAM Support        ON                 YES            26-05-2008 20:53:05.2125
            307                     HSDPA PFRP Scheduler        ON                 YES            26-05-2008 20:53:05.2151
            308       Licence Based BTS channel capacity        ON        32       160       192  26-05-2008 20:53:05.2176
            309                 Antenna Line Supervision        ON                 YES            26-05-2008 20:53:05.2201
            310        Flexi WCDMA BTS Nokia MHA Support        ON                 YES            26-05-2008 20:53:05.2242
            434                                                 ON         0                   3  26-05-2008 20:53:05.2267
            435        Flexi WCDMA BTS 40W Power Licence        ON         0         3         4  26-05-2008 20:53:05.2292
            598                                                 ON         2         0         4  26-05-2008 20:53:05.2321
            616                                IMA (FTM)        ON         0         0         1  26-05-2008 20:53:05.2346
            618                                                 ON         0         0         4  26-05-2008 20:53:05.2373




dmazzini
GSM/UMTS System and Telecomm Consultant

 
First of all, to help yourself and us, you should define clear rules to which your data and field format must adhere to be recognized, and also which ones are required and which ones are not.
As an example, I understand that you have a heading record started with WBTS- and 4 (may be more,may be less?) digits, and that all the following records share the fields of the heading record.
Then you have a TARGETID field that always(?) is composed of an 'L' followed by 10(?) digits.
In your code you are allowing for no space separator between these two fields, but if the space was not there you would mess up your data, as a [tt]\d[/tt] is also a [tt]\S[/tt]: so please decide whether a space must be there or recognize the TARGETID by the starting L (and so on with all the other fields).
I would code in a much safer (and more readable) way as follows (assuming fields as noted):
Code:
use strict;
my $intext;
my($ne_id,$target_id,$internal_id,$dn,$ne_name,$rnc_id,$wbts_id);
my($feature_code,$feature_name,$state,$basic,$licensed,$max,$timestamp);
while($intext=<DATA>){
  chomp$intext;
  $intext=~s/^\s+//;
  $intext=~s/\s+$//;
  $intext=~tr/ / /s; #collapse multiple spaces into one
  if($intext=~/^WBTS-(\d{4})\s/g){
    $ne_id=$1;
    $intext=~/\G(L\d{10})\s/g||die"TARGETID unrecognized in $intext\n";
    $target_id=$1;
      #required field with fixed format
    $intext=~/\G(\d+)\s/g||die"INTERNAL ID unrecognized in $intext\n";
    $internal_id=$1;
      #required field with variable length
    $intext=~/\G(.+?)\s/g||die"DN unrecognized in $intext\n";
    $dn=$1;
      #required field with variable length and format
    $ne_name=substr($intext,pos$intext);
print"$ne_id,$target_id,$internal_id,$dn,$ne_name\n";
  }elsif($intext=~/^(\d{3})\s/g){
    $feature_code=$1;
      #required field with fixed length
    $intext=~/\G(.*?)?(OFF|ON)\s/g||die"STATE unrecognized in $intext\n";
    $feature_name=substr($1,0,-1);
      #not required field
    $state=$2;
      #required field with fixed format
    $intext=~/\G(\d*?\s)?(.*?\s)?(\d*?\s)?(\d{2}-\d{2}-\d{4}\s\d{2}\:\d{2}\:\d{2}\.\d+)$/g||die"TIME unrecognized in $intext\n";
    $basic=substr($1,0,-1);
      #not required field with figures only
    $licensed=substr($2,0,-1);
      #not required field
    $max=substr($3,0,-1);
      #not required field with figures only
    $timestamp=$4;
      #required field with fixed format
print"$feature_name,$state,$basic,$licensed,$max,$timestamp\n";
  }else{
      #Process nonconforming records here (if necessary)
  }
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi

Thanks for you effort working on that.

Regarding your questions:


As an example, I understand that you have a heading record started with WBTS- and 4 (may be more,may be less?) digits, and that all the following records share the fields of the heading record.

Code:
dmazzini: WBTSID may be >1 and < 65535
Then you have a TARGETID field that always(?) is composed of an 'L' followed by 10(?) digits.
Code:
dmazzini: For this particular case yes, but not is a rule.

In your code you are allowing for no space separator between these two fields, but if the space was not there you would mess up your data, as a \d is also a \S: so please decide whether a space must be there or recognize the TARGETID by the starting L (and so on with all the other fields).

Code:
dmazzini: that's the known problem. There should be a way to create a generic regex that determine the amount of records per row, if it changes, it should not mess up the data, just assign null or "NA" values to the "non-existent" record positions. You know create regex without use substr since string lenght may be variable.
I tested the parser with your script and it looks like it's not parsing all info. I will test it again and find why it's not working well.

Cheers



dmazzini
GSM/UMTS System and Telecomm Consultant

 
Just to inform that TARGETID field that always(?) is composed of an 'L' followed by 10(?) digits. That's correct



dmazzini
GSM/UMTS System and Telecomm Consultant

 
It worked for me. However when I paste your __DATA__ into a word processor it loses the field alignment.
And of course there is no way for any code (or a human) to find out whether some field that is not there should have existed or not! Unless of course you have some placeholders (multiple separators, mandatory fields with a fixed pattern,...) that allow to recognize that. So you need to fix your rules before going on, and, if you have a field with a known fixed pattern, don't consider it as a minor item, it's those fields that are going to allow you for recovering your data!.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi

Code below works, I have to split $4 (combination of $basic, $licensed, $state and $timestamp) based on offset and ythen I was able to get
values per variable .I am not really happy with this solution but it works well


Code:
print  "RNCID,NE NAME,WBTSID,TARGET ID,INTERNAL ID,DN,FEATURE CODE,FEATURE NAME,STATE,BASIC,LICENSED,MAX,TIMESTAMP\n";

while (my $intext = <DATA>) { 
	    chomp $intext;		
	    $intext=trim($intext);
	    
	    if ($intext =~ /WBTS\-\d+\s*(L\d{10})\s*(\d+)\s*(PLMN\-PLMN\/RNC\-(\d+)\/WBTS\-(\d+))\s*(\S+)/) {
         ($target_id,$internal_id,$dn,$rnc_id,$wbts_id,$ne_name)=($1,$2,$3,$4,$5,$6);   
      }
      
    
      if ($intext =~ /^(\d+)\s*(.+)?\s*(OFF|ON)(.+)$/) {  
         	$feature_code =$1;         	
         	$feature_name =$2;         	
         	$state =$3;         
          $basic_licensed_max_timestamp=$4;
          
          # e.g value variable after (OFF\ON)  $4         
          #$basic_licensed_max_timestamp='         0         0         3  24-05-2008 10:33:08.1544';
                             
          my $length = 4;
          $basic =  substr $basic_licensed_max_timestamp, 7, $length;        	
          $licensed = substr $basic_licensed_max_timestamp, 17, $length; 
          $max = substr $basic_licensed_max_timestamp, 27, $length;  
          $timestamp = substr $basic_licensed_max_timestamp, 30, 12;      
              
         	$feature_code=trim($feature_code);
          $feature_name=trim($feature_name);          
         	$state=trim($state); 
          $basic=trim($basic); 
         	$licensed= trim($licensed);  
         	$max= trim($max);
         	$timestamp=trim($timestamp);    	
          
          print "$rnc_id,$ne_name,$wbts_id,$target_id,$internal_id,$dn,$feature_code,$feature_name,$state,$basic,$licensed,$max,$timestamp\n";          	          	        
        
      }     
}

sub trim {
    my @out = @_;
    for (@out) {
        s/^\s+//;          # trim left
        s/\s+$//;          # trim right
    }
    return @out == 1 
              ? $out[0]   # only one to return
              : @out;     # or many
}

Now I have noticed something. Look headers and some data


Code:
          NE ID            TARGETID    INTERNAL ID                                                              DN                  NE NAME
   FEATURE CODE                             FEATURE NAME     STATE     BASIC  LICENSED       MAX                      TIME
      WBTS-1695         L6070407474         824477                                     PLMN-PLMN/RNC-115/WBTS-1695                  ToyCell                            FEATURE NAME     STATE     BASIC  LICENSED       MAX                      TIME
            307                     HSDPA PFRP Scheduler        ON                 YES            19-03-2008 17:42:42.7628
            308       Licence Based BTS channel capacity        ON        32       160       192  19-03-2008 17:42:42.7655

I have noticed that where the headers name ends then there (in that position) we have a value.

e.g

Header BASIC, just under letter "C" then I could count 3 positions back and get null for first row and 32 for second row.
Header LICENSED,just under letter "D" then I could 3 positions ang get YES for first row and 160 for second row.

So it would be possible to calculate offset based on headers as well........

Thanks for support.

dmazzini
GSM/UMTS System and Telecomm Consultant

 
So you seem to have just discovered that your data is fixed width right aligned: you just need to determine the widths (this is normally constant across different files containing the same type of data, but you can use, as you state, the headings to determine that), then extract the fields with [tt]substr[/tt] and trim the spaces on the left.
As I told you, a thorough observation of the rules by which the data are organized would have saved much effort.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
It should be easy to parse next files..

Thanks for suppport

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top