Solved

Need help with the Perl script

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

14 Experts available now in Live!

Get 1:1 Help Now