comparison of 2 text files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tilu
    New Member
    • Jun 2007
    • 6

    comparison of 2 text files

    I have two text files. How do I compare using perl?

    -Tilu
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    That depends. In what way do you want to compare them.

    - Miller

    Comment

    • sujay1983
      New Member
      • Jun 2007
      • 2

      #3
      Hi,

      If you are running your script in a unix environment, then better use the diff command.

      `diff file1 file2 > output.txt`

      Comment

      • tilu
        New Member
        • Jun 2007
        • 6

        #4
        Originally posted by miller
        That depends. In what way do you want to compare them.

        - Miller
        I want to write a perl script to compare line by line the two text files.

        Comment

        • Freedolen
          New Member
          • Mar 2007
          • 17

          #5
          You can use any pre-defined modules such as Text::Diff or you can modify/use the following coding

          [CODE=perl]
          use strict;
          use warnings;

          my $firstfile = 'test1.txt';
          my $secondfile = 'test2.txt';

          open(F1, $firstfile) or die "Unable to open $firstfile $!\n";
          open(F2, $secondfile) or die "Unable to open $secondfile $!\n";

          my @firstfile = <F1>;
          my @secondfile = <F2>;

          if (@firstfile != @secondfile) {
          print (@firstfile > @secondfile)
          ? "File $firstfile has more lines than $secondfile\n"
          : "File $secondfile has more lines than $firstfile\n";
          } else {
          for my $i (0..$#firstfile ) {
          if ($firstfile[$i] ne $secondfile[$i]) {
          print "Difference Found in Line-" . ($i+1) . ":\n$firstf ile: $firstfile[$i]\n$secondfile: $secondfile[$i]\n";
          }
          }
          }

          close (F1);
          close (F2);
          [/CODE]
          Last edited by miller; Jun 3 '07, 09:06 PM. Reason: Code Tag and ReFormatting

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by sujay1983
            Hi,

            If you are running your script in a unix environment, then better use the diff command.

            `diff file1 file2 > output.txt`
            Why is that suggestion "better" than using perl?

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by KevinADC
              Why is that suggestion "better" than using perl?
              I would say its not "better" than using Perl, just much quicker as you wouldn't be re-inventing the wheel. Granted, I am one that is all for re-inventing the wheel in order to learn, but if you are needing a quick, right now fix, then I would certainly use the system diff utility.

              jlk

              Comment

              • tilu
                New Member
                • Jun 2007
                • 6

                #8
                Thanks.. Its useful.

                --tilu


                Originally posted by Freedolen
                You can use any pre-defined modules such as Text::Diff or you can modify/use the following coding

                Code:
                use strict;
                use warnings;
                
                my $firstfile = 'test1.txt';
                my $secondfile = 'test2.txt';
                
                open(F1, $firstfile) or die "Unable to open $firstfile $!\n";
                open(F2, $secondfile) or die "Unable to open $secondfile $!\n";
                
                my @firstfile = <F1>;
                my @secondfile = <F2>;
                
                if(scalar(@firstfile) == scalar(@secondfile))
                {
                	for(my $i = 0; $i<=$#firstfile; $i++)
                	{
                		if($firstfile[$i] eq $secondfile[$i])
                		{
                		}
                		else
                		{
                			print "Difference Found in Line-".($i+1).":\n$firstfile: $firstfile[$i]\n$secondfile: $secondfile[$i]\n";
                		}
                	}
                }
                else
                {
                	(scalar(@firstfile)>scalar(@secondfile))?print "File $firstfile has more lines than $secondfile\n":print "File $secondfile has more lines than $firstfile\n";
                }
                close (F1);
                close (F2);

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by numberwhun
                  I would say its not "better" than using Perl, just much quicker as you wouldn't be re-inventing the wheel. Granted, I am one that is all for re-inventing the wheel in order to learn, but if you are needing a quick, right now fix, then I would certainly use the system diff utility.

                  jlk
                  How is it quicker? That would only be true if a person already knows the shell commands and is using an OS that supports it. Nix users seem to think everyone uses nix.

                  Comment

                  • miller
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1086

                    #10
                    Originally posted by tilu
                    I want to write a perl script to compare line by line the two text files.
                    When I asked in what way you wanted to compare them, I suppose I should have been more specific. Generally when comparing files, you're either testing for equality or for difference.

                    For equality, there is a cpan module called File::Compare that I believe would do the job.

                    For difference, the Text::Diff module that Freedolen mentioned would almost certainly do the trick.

                    - Miller

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by KevinADC
                      How is it quicker? That would only be true if a person already knows the shell commands and is using an OS that supports it. Nix users seem to think everyone uses nix.
                      Nix users? ;) I've usually used a wildcard in front of that :D though it doesn't cover Linux quite right.

                      Sorry, but though these utilities came from an OS that is closer to the primordial ooze of computing OSs, there are plenty of gnu ports to most all if not all other OSs platforms.

                      See MinGW or Cygwin for a more packaged implementation, or look for individual binaries.

                      I've also found that once you understand the nix utils, you will find that most of your scripting needs are done for you, so it may be slightly longer short term, but long term you're set.


                      Adrian

                      Comment

                      Working...