Perl script to compare two files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl script to compare two files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2010
dinesh.4126 dinesh.4126 is offline
Registered User
 
Join Date: Sep 2010
Last Activity: 13 September 2010, 12:47 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Perl script to compare two files

hi,
As such I am new to perl on google search I found a code for Perl script to compare two files and print differences between them and instead of prinintg I want to store the diff. in a outputfile so can sombody provide assistance upon this from where can I edit in script to store the diff in output file..

Code:
##!/usr/bin/perl
# file_compare.pl
# Purpose: compare two files and show differences
# usage: file_compare.pl filename1 filename2
use strict;
use warnings;
my $file1 =
  'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\file1.txt';
my $file2 =
  'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\file2.txt';
open( FILE1, "< $file1" ) or die "Can not read file $file1: $! \n";
my @file1_contents = <FILE1>;    # read entire contents of file
close(FILE1);
open( FILE2, "< $file2" ) or die "Can not read file $file2: $! \n";
my @file2_contents = <FILE2>;    # read entire contents of file
close(FILE2);
my $length1 = $#file1_contents;    # number of lines in first file
my $length2 = $#file2_contents;    # number of lines in second file

if ( $length1 > $length2 ) {

    # first file contains more lines than second file
    my $counter2 = 0;
    foreach my $line_file1 (@file1_contents) {
        chomp($line_file1);
        if ( defined( $file2_contents[$counter2] ) ) {

            # line exists in second file
            chomp( my $line_file2 = $file2_contents[$counter2] );
            if ( $line_file1 ne $line_file2 ) {
                print "\nline " . ( $counter2 + 1 ) . " \n";
                print "< $line_file1 \n" if ( $line_file1 ne "" );
                print "--- \n";
                print "> $line_file2 \n\n" if ( $line_file2 ne "" );
            }
        }
        else {

            # there is no line in second file
            print "\nline " . ( $counter2 + 1 ) . " \n";
            print "< $line_file1 \n" if ( $line_file1 ne "" );
            print "--- \n";
            print "> \n";    # this line does not exist in file2
        }
        $counter2++;         # point to the next line in file2
    }
}
else {

    # second file contains more lines than first file
    # or both have equal number of lines
    my $counter1 = 0;
    foreach my $line_file2 (@file2_contents) {
        chomp($line_file2);
        if ( defined( $file1_contents[$counter1] ) ) {

            # line exists in first file
            chomp( my $line_file1 = $file1_contents[$counter1] );
            if ( $line_file1 ne $line_file2 ) {
                print "\nline " . ( $counter1 + 1 ) . " \n";
                print "< $line_file1 \n" if ( $line_file1 ne "" );
                print "--- \n";
                print "> $line_file2 \n" if ( $line_file2 ne "" );
            }
        }
        else {

            # there is no line in first file
            print "\nline " . ( $counter1 + 1 ) . " \n";
            print "< \n";    # this line does not exist in file1
            print "--- \n";
            print "> $line_file2 \n" if ( $line_file2 ne "" );
        }
        $counter1++;         # point to next line in file1
    }
}

Moderator's Comments:
[TABLE] isn't suitable for [CODE]. And correctly formatting your code increases readability a lot


---------- Post updated at 07:45 AM ---------- Previous update was at 07:16 AM ----------

Hi,
Apart from above script I found one more script which will save the diff. in output file.
Suppose a.txt file

315: posedge SYS_CLK_IR)
^^^^^^^
vx1_bit_align.v:315: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
Coding Standard

170: posedge RD_CLK_IR)
^^^^^^^
vx1_ff_dpram.v:170: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
Coding Standard


202: posedge RD_CLK_IR)
^^^^^^^
vx1_fifo_ctrl.v:202: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
Coding Standard


267: posedge PIXEL_CLK_IR)
^^^^^^^
vx1_resizer.v:267: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected

and the second file is a.txt

170: posedge RD_CLK_IR)
^^^^^^^
vx1_ff_dpram.v:170: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
and the diff. of these two will be save in ouput file 1.txt is
vx1_resizer.v:267: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
315: posedge SYS_CLK_IR)
vx1_fifo_ctrl.v:202: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
vx1_bit_align.v:315: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
267: posedge PIXEL_CLK_IR)
202: posedge RD_CLK_IR)

From output file I canclude its printing the diff of these two file in a random manner not in proper sequence as such in input file.so please help me where is error in code because diff of file is printed randomly.
Hope so I cleared my Problem.


Code:
#!/usr/bin/perl -W
use strict;
use warnings; 
my $f1 = 'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\2.txt';
my $f2 = 'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\a.txt';
my $outfile = 'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\1.txt';
my %results = (); 
open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){   $results{$line}=1;
}
close(FILE1); 
open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {  
 $results{$line}++;
}
close(FILE2);  
open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) { print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;



---------- Post updated at 07:46 AM ---------- Previous update was at 07:45 AM ----------

Hi,
Apart from above script I found one more script which will save the diff. in output file.
Suppose a.txt file

315: posedge SYS_CLK_IR)
^^^^^^^
vx1_bit_align.v:315: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
Coding Standard

170: posedge RD_CLK_IR)
^^^^^^^
vx1_ff_dpram.v:170: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
Coding Standard


202: posedge RD_CLK_IR)
^^^^^^^
vx1_fifo_ctrl.v:202: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
Coding Standard


267: posedge PIXEL_CLK_IR)
^^^^^^^
vx1_resizer.v:267: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected

and the second file is 2.txt

170: posedge RD_CLK_IR)
^^^^^^^
vx1_ff_dpram.v:170: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
and the diff. of these two will be save in ouput file 1.txt is
vx1_resizer.v:267: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
315: posedge SYS_CLK_IR)
vx1_fifo_ctrl.v:202: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
vx1_bit_align.v:315: CLOCKS> [WARNING] B_1202: 2 clocks in this unit detected
267: posedge PIXEL_CLK_IR)
202: posedge RD_CLK_IR)

From output file I canclude its printing the diff of these two file in a random manner not in proper sequence as such in input file.so please help me where is error in code because diff of file is printed randomly.
Hope so I cleared my Problem.


Code:
#!/usr/bin/perl -W
use strict;
use warnings; 
my $f1 = 'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\2.txt';
my $f2 = 'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\a.txt';
my $outfile = 'C:\Documents and Settings\dinesh.lohan\Desktop\comparing_file\1.txt';
my %results = (); 
open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){   $results{$line}=1;
}
close(FILE1); 
open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {  
 $results{$line}++;
}
close(FILE2);  
open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) { print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;


Last edited by pludi; 09-13-2010 at 08:26 AM..
Sponsored Links
    #2  
Old 09-13-2010
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 17 April 2014, 10:58 PM EDT
Posts: 1,837
Thanks: 7
Thanked 265 Times in 240 Posts
Quote:
Originally Posted by dinesh.4126 View Post
... instead of prinintg I want to store the diff. in a outputfile ...
...
The code snippet in your post about the meaning of "unless" had the print command to print to an output file. Have a look at that.
Also check the Perl documentation for the "print" function.

tyler_durden
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Script to compare between two baseline in UCM suvenduperl Shell Programming and Scripting 2 08-16-2010 09:31 AM
Use Perl In Bash Script To Compare Floationg Points Grizzly Shell Programming and Scripting 3 10-07-2009 07:40 PM
How to compare csv files using perl kittu1979 UNIX for Dummies Questions & Answers 2 09-24-2008 08:29 AM
Compare 2 csv files in perl kittu1979 UNIX for Dummies Questions & Answers 2 09-24-2008 08:18 AM
compare 2 coloum of 2 diff files using perl script vasuki Shell Programming and Scripting 3 07-30-2008 01:53 AM



All times are GMT -4. The time now is 03:11 PM.