Solved

Perl script to trim Numbers

Posted on 2006-10-31
11
862 Views
Last Modified: 2012-08-14
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
Comment
Question by:Musaab1
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 17844682
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
 
LVL 8

Expert Comment

by:koppcha
ID: 17844951
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
 
LVL 84

Expert Comment

by:ozo
ID: 17844993
where you have
[ *]
did you intend to say
[ ]*
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Musaab1
ID: 17845981
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
 
LVL 84

Expert Comment

by:ozo
ID: 17846232
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 17846267
   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
 
LVL 8

Expert Comment

by:koppcha
ID: 17846275
if( ($_ =~ /^.*?(\d+).*/))
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 250 total points
ID: 17846283
if( ($_ =~ /^.*?(\d+).*/) or ($_ =~ /SDR(\d+)/) )
     {
           $SDR_num = $1;    
           $result = "SDR";
        $result .= "$SDR_num\n";
        print $result;
         
     }
0
 
LVL 8

Accepted Solution

by:
Perl_Diver earned 50 total points
ID: 17847898
while(<>)
{
    print "SDR$1\n"  if   /^\D*(\d+)/;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 17847926
/^.*?(\d+).*/,  /^.*?(\d+).*/ or /SDR(\d+)/,  /^\D*(\d+)/
all get the same $1 as /(\d+)/
(unless $_ contains multiple lines)
0
 
LVL 8

Expert Comment

by:koppcha
ID: 17849379
Ozo,
  I didn't see your post before i did mine :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question