Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl script to trim Numbers

Posted on 2006-10-31
11
Medium Priority
?
888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 800 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 1000 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

719 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