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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.