Hi Monks, I am learning to write code to parse text files using grouped regular expressions. For the following section of code I am trying to find a way to determine if the entire group $1 is found in the input file. If it is I write group $2 to line. I am interested in modifying the code below to write MSISDN=notSet to $line if the entire line group $1 is not set in the code extract below? Thank your for your help
pseudo code check line for tabMSISDN=digits; if found extract digits and assign to line otherwise assign MSISDN=notSet to the line for printing later
Section of code
if (/^(\t*MSISDN=(\d+));/) { print OUTFILE "Update Command $line\n" if defined $line; $line = "<$2>"; #group 2 #otherwise assign MSISDN=notSet to the line for printing later }
full program below
#!/usr/bin/perl use strict; use warnings; my $HSSIN='D:\testproject\sample-input.txt'; my $ofile = 'D:\testproject\sample-output.txt'; my $add; open (INFILE, $HSSIN) or die "Cant open input file"; open (OUTFILE,"> $ofile" ) or die "Cant open file"; my $line; while (<INFILE>) { if (/^(\t*MSISDN=(\d+));/) { print OUTFILE "Update Command $line\n" if defined $line; $line = "<$2>"; #group 2 } if (/(\t*ODBIC=([\w]+?\w.*));/) { #print OUTFILE "$line\n" if defined $line; #$line = $2; $add = $2; $line .= ",$add"; } if (/(\t*ODBOC=([\w]+?\w.*));/) { $add = $2; $line .= ",$add"; } } print OUTFILE "Update Command $line\n"; close INFILE; close OUTFILE;
input file may contain multiple entries
<SUBBEGIN MSISDN=123476789678; ODBIC=BIC; ODBOC=BAOC; <SUBEND <SUBBEGIN ODBIC=BIC; ODBOC=BAOC; <SUBEND
desired output Update Command <123476789678>,BIC,BAOC Update Command MSISDN=notSet,BIC, BAOC
In reply to Grouped Regular Expression not set assign default value by gbwien
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |