Perl- searching for values in one file which could be present in another -
i have file following list of products mentioned :
dl750-12d1 dl750-12d2 dl750-12d3 dl750-12d4 dl750-12d5 dl750-12d6 dl750-12d9 dl750-12d11
i have file contains list of json objects 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 1 search if list of products listed in products page can found in product json page. , there way list if product not present in json page?
i have implemented following code in perl, doesn't fetch me 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"); @file1 = { open $fh, "<", "c:/pannaga/projdocs/prod/products_list.txt" or die "could not open $filename: $!"; <$fh>; }; $count =0; $i (0 .. $#file1) { $count++; $find = $file1[$i]; print out "$count -->line matched $find\n"; @line = { open $fh2, "<", "c:/pannaga/projdocs/prod/products_page_json.txt" or die "could not open $filename: $!"; <$fh2>; }; $j (0 .. $#line) { if (index($line[j], $file1[$i]) != -1) { print "'$line[j]' contains '$file1[$i]'\n"; } } } close(out);
please try this:
use strict; use warnings; use cwd; $lopfile = "listofprod.txt"; $jsonfile = "lop.json"; ($tmp,$jsontxt) = ""; open(lop, $lopfile) || die "can't open file '$lopfile': $!\n"; { local $/; $_ = <lop>; $tmp = $_; } close(lop); @listofprod = split/\s/, $tmp; #print join "\n", @listofprod; open(jsn, $jsonfile) || die "can't open file '$jsonfile': $!\n"; { local $/; $_ = <jsn>; $jsontxt = $_; } close(jsn); open(out, ">report.txt") || die "can't create 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'...
Comments
Post a Comment