Pannaga Gayathri Pannaga Gayathri - 3 months ago 14
Perl Question

Perl- searching for values in one file which could be present in another

I have a file where the following list of products are mentioned :

DL750-12D1
DL750-12D2
DL750-12D3
DL750-12D4
DL750-12D5
DL750-12D6
DL750-12D9
DL750-12D11


I have another file which contains list of JSON objects which are as follows:

{
"Type": "DL750-12D5",
"ProductLevelSimCheck": false,
"HWCompatibilityCheck": true,
"FWVersionCheck": true,
"ConfigCheck": true,
"createdAt": "2016-07-23T04:00:00.000Z",
"Active": true,
"IMEIRequired": true,
"HWCompatibility": "01 01 01 01 01 00 00 00",
"FWVersion": "D6.57",
"Config": "TMC02",
"Generation": "Gen 2",
"ModifiedBy": "chanakyav",
"updatedAt": "2016-07-28T17:42:48.249Z",
"id": "5794182ba6832e7056349c76"
}


How does one search if the list of products listed in products page can be found in product JSON page. And is there a way to list if the product is not present in JSON page?

I have implemented the following code in perl, but doesn't fetch me any results :

#!C:/Dwimperl/perl/bin/perl.exe

use File::Slurp;

#open (PL, "C:/Pannaga/ProjDocs/Prod/products_list.txt");
#open FILE, "<C:/Pannaga/ProjDocs/Prod/products_page_json.txt";
open(Out,'>', "C:/Pannaga/ProjDocs/Prod/Output.txt");


my @file1 = do {
open my $fh, "<", "C:/Pannaga/ProjDocs/Prod/products_list.txt"
or die "could not open $filename: $!";
<$fh>;
};

$count =0;
for my $i (0 .. $#file1)
{

$count++;
$find = $file1[$i];

print Out "$count -->Line that matched $find\n";


my @line = do {
open my $fh2, "<", "C:/Pannaga/ProjDocs/Prod/products_page_json.txt"
or die "could not open $filename: $!";
<$fh2>;
};

for my $j (0 .. $#line) {

if (index($line[j], $file1[$i]) != -1) {
print "'$line[j]' contains '$file1[$i]'\n";
}
}

}

close(Out);

Answer
use Algorithm::Diff qw(diff);

bag("Usage: $0 oldfile newfile") unless @ARGV == 2;

my ($file1, $file2) = @ARGV;

-f $file1 or bag("$file1: not a regular file");
-f $file2 or bag("$file2: not a regular file");

open (F1, $file1) or bag("Couldn't open $file1: $!");
open (F2, $file2) or bag("Couldn't open $file2: $!");
chomp(@f1 = <F1>);
close F1;
chomp(@f2 = <F2>);
close F2;

$diffs = diff(\@f1, \@f2);
exit 0 unless @$diffs;

foreach $chunk (@$diffs) {

    foreach $line (@$chunk) {

        my ($sign, $lineno, $text) = @$line;
        printf "%4d$sign %s\n", $lineno+1, $text;
    }
}
exit 1;

sub bag {
   my $msg = shift;
   $msg .= "\n";
   warn $msg;
   exit 2;
}