Solved

Perl script to trim Numbers

Posted on 2006-10-31
11
852 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
Comment Utility
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
Comment Utility
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
Comment Utility
where you have
[ *]
did you intend to say
[ ]*
0
 

Author Comment

by:Musaab1
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
Comment Utility
   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
Comment Utility
if( ($_ =~ /^.*?(\d+).*/))
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 250 total points
Comment Utility
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
Comment Utility
while(<>)
{
    print "SDR$1\n"  if   /^\D*(\d+)/;
}
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
/^.*?(\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
Comment Utility
Ozo,
  I didn't see your post before i did mine :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now