Solved

Need help with the Perl script

Posted on 2014-09-09
1
148 Views
Last Modified: 2014-09-09
Below is the compare script which compares strings in two text files.  The two attached files are workspace.txt and invalids.txt. This script checks if the string in workspace.txt is present in invalids.txt. If a match is found then corresponding line in invalids.txt is printed or else the string from workspace.txt is printed.

For example:

The above check in invalids.txt is to look for string between the dot(.)and colon(') at the end.
       
For instance in the file invalids.txt consider the line
'PACKAGE BODY: TRON2000.ts_k_jrp_300901sfsfs0_mma'      

For comparison only consider the string

ts_k_jrp_300901sfsfs0_mma

======If a match is found then print the complete corresponding line in invalids.txt  like


     'PACKAGE BODY: TRON2000.ts_k_jrp_300901sfsfs0_mma' is invalid

      or else just print the text from workspace.txt is valid ....in this case print

      ts_k_jrp_300901sfsfs0_mma is valid


Below is the script .


use strict;
use warnings;
use Fatal qw(open close);
my $finv = shift or die "Usage: $0 <invalids_file> <workspace_file>\n";
my $fwrk = shift or die "Usage: $0 <invalids_file> <workspace_file>\n";
open IN, $finv or die "could not open $finv: $!";
my %chk;
while (<IN>) {
    chomp;
    next if m{^\d+\s+rows?\s*$};
    if (m{\.(.*)'\s*$}) {
        $chk{$1} = $_;
    }
}
close IN;
open IN, $fwrk or die "could not open $fwrk: $!";
while (<IN>) {
    chomp;
    if (exists $chk{$_}) {
        print "$chk{$_} is invalid\n";
    } else {
        print "$_ is valid\n";
    }
}
close IN;
                           

Open in new window


##########Need a modification in the above script to handle uppercase strings in invalids.txt like


'PACKAGE BODY: TRON2000.TS_K_JRP_300901SFSF0_MMA'

The call to the script is  test_compare.pl path/to/invalids.txt path/to/workspace.txt
workspace.txt
invalids.txt
0
Comment
Question by:gaurav sharma
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40312265
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 <invalids_file> <workspace_file>\n" unless @ARGV==2;
use Fatal qw(open close);
my %chk;
while( <> ){
    s/\s+$//;
    @ARGV &&  m{\.(.*)'$} and $chk{lc $1} = $_;
    @ARGV || print $chk{lc $_}?"$chk{lc $_} is invalid\n":"$_ is valid\n";
}

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

22 Experts available now in Live!

Get 1:1 Help Now