Perl-在一个文件中搜索可能存在于另一个文件中的值

时间:2016-08-08 14:23:37

标签: perl

我有一个文件,其中提到了以下产品列表:

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

我有另一个文件,其中包含JSON对象列表,如下所示:

 {
      "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"
    }

如何在产品JSON页面中找到产品页面中列出的产品列表,如何搜索。有没有办法列出JSON页面中是否没有该产品?

我在perl中实现了以下代码,但是没有获取任何结果:

#!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);

2 个答案:

答案 0 :(得分:1)

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;
}

答案 1 :(得分:0)

请试试这个:

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);

输出:

 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'...