Solved

Need help with the Perl script

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

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

732 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