in reply to Re^2: Grouped Regular Expression not set assign default value
in thread Grouped Regular Expression not set assign default value

Hello again gbwien,

I am not 100% sure what is the expected output from your description. I took a guess and I put together an example based on the new input and what I think you mean.

Having said that I think the only modifications that you need to add is:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; open (my $fhIn, '<', "in.txt") or die "Can not open 'in.txt': $!"; open (my $fhOut, '>', "out.txt") or die "Can not open 'out.txt': $!"; my %hash = (); my $count = 0; while (<$fhIn>) { chomp; next if (/<BEGINFILE>/); if (/<SUBBEGIN/) { $count = 1; } elsif (/<SUBEND/) { $count = 0; } elsif ($count) { my @tmp = split /=/; chop $tmp[1]; if (/MSISDN/) { $hash{MSISDN} = $tmp[1]; } elsif (/ODBIC/) { $hash{ODBIC} = $tmp[1]; } elsif (/ODBOC/) { $hash{ODBOC} = $tmp[1]; } elsif (/CF/) { my @cf = split /-/, $tmp[1]; if (index($tmp[1], 'CFB-ALL-PROV-NONE') != -1){ $hash{'CF'}{$cf[0]} = 'CfbNotSet'; } elsif (index($tmp[1], 'CFU-ALL-PROV-NONE') != -1){ $hash{'CF'}{$cf[0]} = 'CfuNotSet'; } elsif (index($tmp[1], 'CFNRY-ALL-PROV-NONE') != -1){ $hash{'CF'}{$cf[0]} = 'CfnrnyNotSet'; } elsif (index($tmp[1], 'CFNRC-ALL-PROV-NONE') != -1){ $hash{'CF'}{$cf[0]} = 'CfnrncNotSet'; } else { $hash{'CF'}{$cf[0]} = $tmp[1]; } } } if ($count == 0) { print Dumper \%hash; if (exists $hash{MSISDN}) { say $fhOut "Update Command <".$hash{MSISDN}.">,".$hash{ODBIC}. +",".$hash{ODBOC}.""; } else { say $fhOut "Update Command MSISDN=notSet,".$hash{ODBIC}.",".$h +ash{ODBOC}.""; } delete $hash{MSISDN}; delete $hash{ODBIC}; delete $hash{ODBOC}; delete $hash{CF}; } } close ($fhIn) or warn "Could not close 'in.txt': $!"; close ($fhOut) or warn "Could not close 'out.txt': $!"; __END__ $ perl test.pl $VAR1 = { 'MSISDN' => '431234567893', 'ODBOC' => 'BAOC', 'ODBIC' => 'BAIC', 'CF' => { 'CFNRC' => 'CfnrncNotSet', 'CFB' => 'CfbNotSet', 'CFD' => 'CFD-TS10-ACT-91436903000-YES-YES-25-YES- +65535-YES-YES-NO-NO-NO-YES-YES-YES-YES-NO;', 'CFNRY' => 'CfnrnyNotSet', 'CFU' => 'CfuNotSet' } }; $VAR1 = { 'ODBIC' => 'BICCROSSDOMESTIC', 'ODBOC' => 'BAOC', 'MSISDN' => '431234567899', 'CF' => { 'CFNRY' => 'CfnrnyNotSet', 'CFU' => 'CFU-TS10-ACT-914369050045021-YES-NO-NONE +-YES-65535-YES-YES-NO-NO-NO-NO-NO-NO-NO-NO', 'CFB' => 'CfbNotSet', 'CFNRC' => 'CfnrncNotSet', 'CFD' => 'CFD-TS10-REG-91436903000-YES-YES-25-YES- +65535-YES-YES-NO-NO-NO-YES-YES-YES-YES-NO' } }; $ cat out.txt Update Command <431234567893>,BAIC,BAOC Update Command <431234567899>,BICCROSSDOMESTIC,BAOC

I have not update the concatenation of the string output since I do not know what is the actual output that you are looking for.

In case that this is not the expected way the string should behave, provide an desired output sample so I can understand approximately based on input which fields you want to keep and which you want to skip.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^4: Grouped Regular Expression not set assign default value
by gbwien (Sexton) on Feb 23, 2018 at 12:16 UTC

    Hi thanos1983 An example of an input file is below, the file starts with <BEGINFILE> and ends with <ENDFILE>. To accommodate for this I added to your while loop

    chomp; next if /^<ENDFILE>/; next if /^<BEGINFILE>/; next if (/<BEGINFILE>/);
    <BEGINFILE> <SUBBEGIN IMSI=0000000000000; MSISDN=4411287879987; DEFCALL=TS11; CURRENTNAM=BOTH; CAT=COMMON; TBS=TS11&TS12&TS21&TS22; VLRLIST=10; SGSNLIST=10; MSC=445454578142; SGSN=44546578751; SMDP=MSC; CB=BAOC-ALL-PROV; CB=BOIC-ALL-PROV; CB=BOICEXHC-ALL-PROV; CB=BICROAM-ALL-PROV; IMEISV=000011222554545; CW=CW-ALL-PROV; CF=CFU-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO-NO +-NO-NO; CF=CFB-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO-N +O-NO-NO; CF=CFNRY-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO +-NO-NO-NO; CF=CFNRC-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO- +NO-NO-NO; CF=CFD-TS10-ACT-9112345678-YES-YES-25-YES-65535-YES-YES-NO-NO-NO-Y +ES-YES-YES-YES-NO; CONTROL=SUB; WPA=0; GS=HOLD&MPTY&ECT&CLIR&CLIP; CLIRES=TEMPALLOW; CLIPOC=NO; ARD=PROV; SUBRES=ALLPLMN; IST_ALERT_TIMER=120; IST_ALERT_RESPONSE=2; SUB_AGE=0; UPL_TIME=1447410859; PURGE_TIME_ATMSC=1505118714; GPRSUPL_TIME=1446458542; PURGE_TIME_ATSGSN=1447506399; MIMSI=564567467234544-ONELIVE-1-1-1-0-0; MIMSI=123245748722558-ONELIVE-2-2-1-0-0; SID=64672546767; UNRI=NO; UNRR=100; URRP_MME=REACHABLE; URRP_SGSN=NOT_REACHABLE; MCSISTATE=YES; CLRBSG=CLIP-YES-NO-NO-NO-NO; UPLCSLCK=NO; UPLPSLCK=NO; DEFOFAID=10; EPS_PROFILE_ID=1; TGPPAMBRMAXUL=50000000; TGPPAMBRMAXDL=150000000; ARD_EXT=NULL-NULL-NULL-N3GPPNOTALLOWED; SUB_DYN_EPS_ID=1; MMEHOST=graz1.mme.epc.mnc003.mcc232.3gppnetwork.org; MMEREALM=epc.mnc003.mcc232.3gppnetwork.org; EPS_IMEI=866695022931980; EPS_IMEISV=00; MMEFEATURELIST=ALL_APN-HPLMN_APN-VPLMN_APN-NULL-NULL-NULL-NULL-NUL +L-NULL-REGSUB-TRACE-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL +-NULL-NULL-NULL-NULL-NULL-TADS_DATA_RETR; SGSNFEATURELIST=NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL- +NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL-NULL +-NULL-NULL-NULL; MMEULRFLAG=SING_REG_IND-S6AORS6D_IND-SKIP_SUBDATA-NULL-NULL-NULL-P +S_LCS_NOTSUPP; SGSNULRFLAG=NULL-NULL-NULL-NULL-NULL-NULL-NULL; MMETIMESTAMP=1447506399; S4SGSNTIMESTAMP=0; FRAUDTPL_ID=10; HLR_INDEX=1; LTEAUTOPROV=NO; PSSER=1-1-10-1-NONE-DYNAMIC-00000000; EPSSER=1-10-10-1-NONE-DYNAMIC-00000000-1; MPS=NO; ODBIC=BAIC; ODBOC=BAOC; ODBROAM=ODBOHC; ODBPRC=ENTER; ODBPRC=INFO; ODBPLMN=NONE; ODBPOS=NOBPOS-BOTH; ODBECT=OdbAllECT; ODBDECT=YES; ODBMECT=YES; ODBPREMSMS=YES; ODBADULTSMS=YES; <SUBEND <ENDFILE>

    The output should look like this for this record,couple of regex do work on the output CFU-ALL-PROV-NONE becomes CFU-ALL-PROV-1/1/1/0,CFB-ALL-PROV-NONE becomes CFNRY-ALL-PROV-1/1/1/0,,CFNRY-ALL-PROV-NONE becomes CFNRY-ALL-PROV-1/1/1/0, CFD-TS10-ACT-9112345678 becomes CFD-TS10-ACT-12345678 The ouput consists of the following values, MSISDN,CB,CF and ODB values.

    4411287879987,BAOC-ALL-PROV,BOIC-ALL-PROV,BOICEXHC-ALL-PROV,BICROAM-ALL-PROV,CFU-ALL-PROV-1/1/1/0,CFB-ALL-PROV-1/1/1/0,CFNRY-ALL-PROV-1/1/1/0,CFNRC-ALL-PROV-1/1/1/0,CFD-TS10-ACT-1/1/1/12345678,BAIC,BAOC,ODBOHC,ENTER,INFO,NONE,NOBPOS-BOTH,OdbAllECT,YES,YES,YES,YES

    Data::Dumper shows the following but CFNRC, CFB, CFU and CFNRY exist in the input file so the values for these keys are incorrect

    $VAR1 = { 'ODBOC' => 'BAOC', 'ODBIC' => 'BAIC', 'CF' => { 'CFNRC' => 'CfnrncNotSet', 'CFB' => 'CfbNotSet', 'CFD' => 'CFD-TS10-ACT-9112345678-YES-YES-25-YES-6 +5535-YES-YES-NO-NO-NO-YES-YES-YES-YES-NO', 'CFU' => 'CfuNotSet', 'CFNRY' => 'CfnrnyNotSet' }, 'MSISDN' => '4411287879987' };
    Thanks again

      Hello again gbwien,

      I was under the impression that you want the opposite output. This should do what you want:

      Hope this helps. BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Thanks Thanos, I will try that and get back to you Graham