Solved

Need help with the Perl script

Posted on 2014-09-09
1
153 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
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…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

20 Experts available now in Live!

Get 1:1 Help Now