http://www.perlmonks.org?node_id=995540

renjoooz has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 log files as given below.Both files have some things in common.I need 1 file having the common items and another file having items which are not common to both. Thanks in advance.

FILE1: <CMD> deselectAll <CMD> uiSetTool ruler <CMD> uiSetTool select dummy_core/pads/dfdtghj_op dummy_core/pads/fghf_bg <CMD> windowSelect 5522.2554.33654.256552.44 <CMD> spaceObject -fixSide bottom -space 0 Spacing instances ... selected instances/modules: dummy_core/pads/xx dummy_core/pads/hg_2 dummy_core/pads/dhghj_uu <CMD> deselectAll <CMD> selectInst core/pads/IO_gnd_right18 <CMD> selectInst core/pads/IO_vdd_right18 <CMD> uiSetTool move5 dummy_core/pads/gh_5 dummycore/pads/ghjk_0 dummy_core/pads/ghjk_8 FILE2: 1. this file is for error checking 2. this file is for error checking dummy_core/pads/dfdtghj_op dummy_core/pads/fghf_bg dummy_core/pads/xx dummy_core/pads/hg_2 <CMD> utTl ruler <CMD> utTl select dummy_core/pads/dhghj_uu dummy_core/pads/gh_5 dummycore/pads/ghjk_0 pads/fghf_bg pads/dhghj_u dummy_core/pads/ghjk_8 RESULT NOTCOMMON: <CMD> deselectAll <CMD> uiSetTool ruler <CMD> uiSetTool select <CMD> windowSelect 5522.2554.33654.256552.44 <CMD> spaceObject -fixSide bottom -space 0 Spacing instances ... selected instances/modules: <CMD> deselectAll <CMD> selectInst core/pads/IO_gnd_right18 <CMD> selectInst core/pads/IO_vdd_right18 <CMD> uiSetTool move5 1. this file is for error checking 2. this file is for error checking <CMD> utTl ruler <CMD> utTl select pads/fghf_bg pads/dhghj_u RESULT COMMON: dummy_core/pads/dfdtghj_op dummy_core/pads/fghf_bg dummy_core/pads/xx dummy_core/pads/hg_2 dummy_core/pads/dhghj_uu dummy_core/pads/gh_5 dummycore/pads/ghjk_0 dummy_core/pads/ghjk_8

I have tried different ways to get the result...but none of them was successful... I am providing some of the codes i have tried

#!/usr/bin/perl $fnx = "/home/perl/t1x.g"; $fny = "/home/perl/t25y.g"; $fnz = "/home/perl/t22z.g"; $fntmp = "/home/perl/t22tmp.g"; open FPX, "$fnx" or die "Error in write:$!"; open FPY, "$fny" or die "Error in write:$!"; open FPZ, ">$fnz" or die "Error in write:$!"; open FPTMP, ">$fntmp" or die "Error in write:$!"; while (<FPX>) { @x_line = $_; while (<FPY>) { if($_ == @x_line){ print "Saving matched at $.\n"; print FPTMP "$_ \n"; } else{ print "Saving unmatched at $.\n"; print FPZ "$_ \n"; } } } close FPX; close FPY; close FPZ; #!/usr/bin/perl $fnx = "/home/perl/t1x.g"; $fny = "/home/perl/t25y.g"; $fnz = "/home/perl/t22z.g"; $fntmp = "/home/perl/t22tmp.g"; open FPX, "$fnx" or die "Error in write:$!"; open FPZ, ">$fnz" or die "Error in write:$!"; open FPTMP, ">$fntmp" or die "Error in write:$!"; XFILE: $flag = 0; while ($x_line = <FPX>) { open FPY, "$fny" or die "Error in write:$!"; while ($y_line = <FPY>) { if($y_line eq $x_line) { print "Match found at $y_line\n"; print FPTMP "$y_line\n"; $flag = 1; next XFILE; } else { print "Match not found at $.\n"; next; } } if ($flag == 0) { print "Some Match not found at $x_line\n"; print FPZ "$x_line\n"; } close FPY; } close FPX; close FPZ; close FPTMP; #!/usr/bin/perl $fnx = "/home/perl/t1x.g"; $fny = "/home/perl/t25y.g"; $fnz = "/home/perl/t22z.g"; $fntmp = "/home/perl/t22tmp.g"; open FPX, "$fnx" or die "Error in write:$!"; open FPY, "$fny" or die "Error in write:$!"; open FPZ, ">$fnz" or die "Error in write:$!"; open FPTMP, ">$fntmp" or die "Error in write:$!"; while (<FPX>) { @x_line = $_; while (<FPY>) { if($_ =~ m/@x_line/){ @bb = m/@x_line/; print FPTMP "@bb\n"; next; } else { print FPZ "@x_line"; next; } } } close FPX; close FPY; close FPZ; #!/usr/bin/perl $fnx = "/home/t1x.g"; $fny = "/home/t25y.g"; $fnz = "/home/t22z.g"; $fntmp = "/home/t22tmp.g"; open FPX, "$fnx" or die "couldn't open $fnx: $!"; open FPZ, ">$fnz" or die "couldn't open $fnz: $!"; open FPTMP, ">$fntmp" or die "couldn't open $fntmp: $!"; XFILE:while (@x_line = <FPX>) { open FPY, "$fny" or die "Error in write:$!"; while (@y_line = <FPY>) { if (@x_line eq @y_line){ print FPTMP "@x_line\n"; goto XFILE; } else{ print FPZ "@y_line\n"; } } close FPY; } close FPX; close FPZ; close FPTMP;

Replies are listed 'Best First'.
Re: Need help for extracting Log file using Perl script
by McA (Priest) on Sep 25, 2012 at 13:26 UTC

    Hi!

    Look at this. Probably it helps to find the way.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $filename1 = 'file1.txt'; my $filename2 = 'file2.txt'; open my $fhin1, "<", $filename1 or die "ERROR: Can't open file '$filen +ame1': $!"; open my $fhin2, "<", $filename2 or die "ERROR: Can't open file '$filen +ame2': $!"; my %lines_found_in_file1 = map { chomp; s/^\s+//; s/\s+$//; $_ => 1 } (<$fhin1>); my %lines_found_in_file2 = map { chomp; s/^\s+//; s/\s+$//; $_ => 1 } (<$fhin2>); close $fhin1 or die "ERROR: Couldn't close file '$filename1': $!"; close $fhin2 or die "ERROR: Couldn't close file '$filename2': $!"; my $output1 = 'common.txt'; my $output2 = 'uncommon.txt'; open my $fhout1, ">", $output1 or die "ERROR: Can't open file '$output +1': $!"; open my $fhout2, ">", $output2 or die "ERROR: Can't open file '$output +2': $!"; # Compare in one direction foreach my $line (keys %lines_found_in_file1) { if(exists $lines_found_in_file2{$line}) { print $fhout1 $line, "\n"; } else { print $fhout2 $line, "\n"; } } # Compare in other direction foreach my $line (keys %lines_found_in_file2) { if(exists $lines_found_in_file1{$line}) { # Don't do anything as you printed these lines in the first go } else { print $fhout2 $line, "\n"; } } close $fhout1 or die "ERROR: Couldn't close file '$output1': $!"; close $fhout2 or die "ERROR: Couldn't close file '$output2': $!";

    Best regards
    McA

Re: Need help for extracting Log file using Perl script
by grizzley (Chaplain) on Sep 25, 2012 at 13:31 UTC
    Assuming that there are no two equal lines in one file and files are not too big two simple loops will do the job:
    #!/usr/bin/perl $fnx = "file1"; $fny = "file2"; $fnz = "file.uncommon"; $fntmp = "file.common"; open FPX, "$fnx" or die "Error in write:$!"; open FPY, "$fny" or die "Error in write:$!"; open FPZ, ">$fnz" or die "Error in write:$!"; open FPTMP, ">$fntmp" or die "Error in write:$!"; @x_lines = map {s/^\s+//; s/\s+$//; $_ } <FPX>; @y_lines = map {s/^\s+//; s/\s+$//; $_ } <FPY>; for $xnr(1..@x_lines) { $x = $x_lines[$xnr-1]; $matched = 0; for $ynr(1..@y_lines) { $y = $y_lines[$ynr-1]; if($x eq $y) { print "Saving matched at $xnr\n"; print FPTMP "$x \n"; $matched = 1; last; } } if(!$matched) { print "Saving unmatched at $xnr\n"; print FPZ "$x \n"; } } close FPX; close FPY; close FPZ;