Perl script to trim Numbers

here is the perl script. the functionality is to look into the text files and pull out the numbers and wrtite SDR infront of them. e.g suppose if a number is 95846 it will pull it out and write as SDR95846 and puts the result in the text file. The only issue is if the number is uptil five didgits it runs well but if it is 6 digits it does not pull it. If you can please modify the code so that it pulls the number having six digits.

Thanks



use warnings;
use strict;
use Getopt::Long;

our $VERSION = 0.1;

my $version;
my $help;

# Get the options from the command line                              
GetOptions(      'help|?' => \$help,
                  'version' => \$version );
                  
# Short version information.
if( $version )
{
      print "SDR Filter -- Version $VERSION\n";

      exit(1);
}      # if

# Output help info
outputUsage() if $help || $#ARGV;

my $output_string = undef;
my $SDR_num;
my $result;
my $counter;
      
while(<>)
{
       chomp;            # avoid \n on last field
      #s/^\s+//;      # Strip leading & trailing whitespace
      #s/\s+$//;

      # Sample lines:            
      # >SDR3463<
      # DevTracks: SDR24002

       
     
      if( ($_ =~ /[^M][ ][ *][ ](\d+)[ ][ ][ ](\d+)(\s+)(\w)/) or ($_ =~ /SDR(\d+)/) )
      {
           $SDR_num = $1;      
           $result = "SDR";
         $result .= "$SDR_num\n";
         print $result;
            
      }      #if
     

               


}      # while



#Output the Usage message
sub outputUsage
{
      print <<END;
Musaab1Asked:
Who is Participating?
 
Perl_DiverConnect With a Mentor Commented:
while(<>)
{
    print "SDR$1\n"  if   /^\D*(\d+)/;
}
0
 
Perl_DiverCommented:
post sample lines from the file, there is no reason why the regexp should stop at 5 digits unless the digits are broken up.
0
 
koppchaCommented:
As Perl Diver suggested please post the sample lines in the file...PLease try this as well..
if( ($_ =~ /[^M].*(\d+).*(\d+)(\s+)(\w)/) or ($_ =~ /SDR(\d+)/) ) instead of what you  have
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ozoCommented:
where you have
[ *]
did you intend to say
[ ]*
0
 
Musaab1Author Commented:
Koppcha that line didnt work at all but here is how the data looks like, it is only supposed to pick the first column and put SDR infront of them its does everything perfectly except it does pick the ones with six digits. is this the reason they start with1.


    91905   989013  Verification      
    91914   989013  Verification      
    91920   989013  Verification      
    91925   989013  Verification      
    91930   989013  Verification      
    93724   989013  Verification      
  * 94760   987362  Integration        
    96802   983376  Verification      
  * 97837   985979  Integration        
    99573   985186  Verification      
    99792   984581  Verification      
    100560  984415  Verification      
    100880  985961  Verification      
    101726  927021  Investigation      
  * 102014  985827  Integration        
 
0
 
ozoCommented:
while( <DATA> ){
   print "SDR$1\n" if /(\d+)/;
}
__DATA__
   91905   989013  Verification      
    91914   989013  Verification      
    91920   989013  Verification      
    91925   989013  Verification      
    91930   989013  Verification      
    93724   989013  Verification      
  * 94760   987362  Integration        
    96802   983376  Verification      
  * 97837   985979  Integration        
    99573   985186  Verification      
    99792   984581  Verification      
    100560  984415  Verification      
    100880  985961  Verification      
    101726  927021  Investigation      
  * 102014  985827  Integration        
0
 
ozoConnect With a Mentor Commented:
   100560  984415  Verification      
    100880  985961  Verification      
    101726  927021  Investigation      
  * 102014  985827  Integration      
match /(\d+)[ ][ ](\d+)(\s+)(\w)/ not /(\d+)[ ][ ][ ](\d+)(\s+)(\w)/
you might use / +/ or / {2,3}/ or /\s+/ instead of /[ ][ ][ ]/
0
 
koppchaCommented:
if( ($_ =~ /^.*?(\d+).*/))
0
 
koppchaConnect With a Mentor Commented:
if( ($_ =~ /^.*?(\d+).*/) or ($_ =~ /SDR(\d+)/) )
     {
           $SDR_num = $1;    
           $result = "SDR";
        $result .= "$SDR_num\n";
        print $result;
         
     }
0
 
ozoCommented:
/^.*?(\d+).*/,  /^.*?(\d+).*/ or /SDR(\d+)/,  /^\D*(\d+)/
all get the same $1 as /(\d+)/
(unless $_ contains multiple lines)
0
 
koppchaCommented:
Ozo,
  I didn't see your post before i did mine :)
0
All Courses

From novice to tech pro — start learning today.