in reply to Re: Compare two log files line by line containing a keyword
in thread Compare two log files line by line containing a keyword

You understood the question correctly and your output is quite good also, but I'm facing this issue while run. One thing more I want this output in a separate output file, please change the code accordingly.

Can't locate Data/Dump.pm in @INC (@INC contains: /usr/local/lib64/per +l5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/per +l5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at script3.pl lin +e 4. BEGIN failed--compilation aborted at script3.pl line 4

Code I am trying to run is

#!/usr/bin/perl; use strict; use warnings; use Data::Dump qw(pp); open(FILE, "<x.log"); my @array = <FILE>; close(FILE); open(FILE, "<y.log"); my @array1 = <FILE>; close(FILE); my @array =<<'EOF' =~ m/(^.*\n)/mg; EOF my @array1 =<<'EOF2' =~ m/(^.*\n)/mg; EOF2 @array = map{/^.*(mti_clk_chk:.*\n)/;$1;}@array; @array1 = map{/^.*(mti_clk_chk:.*\n)/;$1;}@array1; ## just print these arrays out so that we can see how to compare them? my $n=1; foreach (@array) {print "A$n) ",$_; $n++} print "===============================\n"; $n=1; foreach (@array1) {print "B$n) ",$_; $n++} ## Make hash tables of the 2 records ## regex could be better - not the point here.. just a quick demo of a +n approach.. my %A = map{/mti_clk_chk:(?::main :|INFO:)?\s*(.*).*\s*T=(\d+)$/;$1 => + $2}@array; my %B = map{/mti_clk_chk:(?::main :|INFO:)?\s*(.*).*\s*T=(\d+)$/;$1 => + $2}@array1; #print only common values in sequential order of file A print "\n********************************************\n"; print "Common Text descriptions with A and B T values:\n"; print "********************************************\n"; foreach my $mti (map{/mti_clk_chk:(?::main :|INFO:)?\s*(.*).*\s*T=(\d+ +)$/;$1}@array) { if (exists $B{$mti}) { print "$mti T(a)=$A{$mti} T(b)=$B{$mti}\n"; } }

Replies are listed 'Best First'.
Re^3: Compare two log files line by line containing a keyword
by thanos1983 (Parson) on Feb 20, 2019 at 09:28 UTC

    Hello again rahu_6697,

    Although fellow Monk Marshall spend for free his time to implement a solution for your problem you do not even show the minimum amount of time and effort.

    You are saying that the script is not compiling because of:

    Can't locate Data/Dump.pm in @INC (@INC contains: /usr/local/lib64/per +l5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/per +l5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at script3.pl lin +e 4. BEGIN failed--compilation aborted at script3.pl line 4

    Did you spend even 5 seconds to search on the web regarding this error? Can't locate ... in @INC

    Even this was not enough you ask him to modify his code so you can get the desired output into a file.

    One thing more I want this output in a separate output file, please change the code accordingly.

    Seriously if you want to learn even the basics regarding Perl and in generally scripting I would recommend start trying and failing until you get it right. Asking for other to resolve your problem will not teach you anything.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^3: Compare two log files line by line containing a keyword
by Marshall (Canon) on Feb 22, 2019 at 01:16 UTC
    As other Monks have pointed out, the error you are getting is about Data::Dump not being installed. I used one of the subroutines in that module (pp) while fiddling with your problem. If you had investigated what pp() does and carefully read my code, you would have discovered that I don't even use that subroutine in the code version that I posted. So #use Data::Dump qw(pp); and my code still runs!

    Please continue to spend effort on your own code. Do not expect final polished code from this forum. My objective was to demo an approach that probably hadn't occurred to you. I believe that I was successful in that endeavour. The Monks do expect that you spend some serious effort trying to understand the advice that you have been given.

    Update: Homework for the OP:
    1) Open and read log file X, line by line and create a hash %X like I show as %A. Hashes have no order to them so we need an array to maintain the input order. Save each key to hash %X as a new element of an array (i.e push that key onto another array).
    2) Open and read log file Y, line by line and create a hash %Y like I show as %B. There is no need for an array here.
    3) Now loop over the array from step (1) and print stuff that is common between hash %X and %Y.

    This is not what you need:

    open(FILE, "<x.log"); my @array = <FILE>; # no need to use memory that way # a lot of the input line will be # thrown away close(FILE);
    Better:
    open(FILE, "<", "x.log") or die "unable to open x.log $!"; while my $line (<FILE>) { ... process each $line... }
Re^3: Compare two log files line by line containing a keyword
by karlgoethebier (Abbot) on Feb 21, 2019 at 15:35 UTC

    See here for help.

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help