Reputation: 1
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);
Upvotes: 0
Views: 70
Reputation: 911
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;
}
Upvotes: 1
Reputation: 2589
Please try this:
use strict;
use warnings;
use Cwd;
my $lopfile = "listofprod.txt";
my $jsonfile = "LOP.json";
my ($tmp,$jsontxt) = "";
open(LOP, $lopfile) || die "Can't open the file '$lopfile': $!\n";
{ local $/; $_ = <LOP>; $tmp = $_; }
close(LOP);
my @listofprod = split/\s/, $tmp;
#print join "\n", @listofprod;
open(JSN, $jsonfile) || die "Can't open the file '$jsonfile': $!\n";
{ local $/; $_ = <JSN>; $jsontxt = $_; }
close(JSN);
open(OUT, ">Report.txt") || die "Can't create the file 'Report.txt': $!\n";
for(0..$#listofprod)
{
if($jsontxt=~m/$listofprod[$_]/g)
{
print OUT "YES: It's matched '$listofprod[$_]'...\n";
}
else
{
print OUT "NO: It's not matched '$listofprod[$_]'...\n";
}
}
close(OUT);
OUTPUT:
NO: It's not matched 'DL750-12D1'...
NO: It's not matched 'DL750-12D2'...
YES: It's matched 'DL750-12D3'...
NO: It's not matched 'DL750-12D4'...
YES: It's matched 'DL750-12D5'...
NO: It's not matched 'DL750-12D6'...
YES: It's matched 'DL750-12D9'...
NO: It's not matched 'DL750-12D11'...
Upvotes: 0