Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

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;
0
Musaab1
Asked:
Musaab1
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
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
 
ozoCommented:
where you have
[ *]
did you intend to say
[ ]*
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
ozoCommented:
   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
 
koppchaCommented:
if( ($_ =~ /^.*?(\d+).*/) or ($_ =~ /SDR(\d+)/) )
     {
           $SDR_num = $1;    
           $result = "SDR";
        $result .= "$SDR_num\n";
        print $result;
         
     }
0
 
Perl_DiverCommented:
while(<>)
{
    print "SDR$1\n"  if   /^\D*(\d+)/;
}
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now