Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: compare two files and print the differences of file 2 in a third file

by kevbot (Vicar)
on Jun 14, 2017 at 06:04 UTC ( [id://1192754]=note: print w/replies, xml ) Need Help??


in reply to compare two files and print the differences of file 2 in a third file

Hello lonnie,

I'm not sure that I completely understand your example. The data in GoodFile.txt and BadFile.txt differ on line 2, so I would expect that difference to show up in Output.txt. However, in your example it does not.

Here is some code that uses Algorithm::Diff to calculate the diff, with some help from Path::Tiny to get the file contents.

This code,
#!/usr/bin/env perl use strict; use warnings; use Path::Tiny; use Algorithm::Diff qw/diff/; my @lines1 = path('GoodFile.txt')->lines; my @lines2 = path('BadFile.txt')->lines; my $diff = Algorithm::Diff->new( \@lines1, \@lines2 ); while( $diff->Next() ){ next if $diff->Same(); print $diff->Items(2), "\n"; } exit;
gives the following output:
This section is the description of the animal bla bla lbas.....bla bla + lbas..... Amimal, cat, 3, 4, YELLOW LEG 3, HIGH 'this is a cattt 1 This section is the description of the animal bla bla lbas.....bla bla + lbas blaaaal........

UPDATE: The OP made edits to their node, and removed their input data. Here is the input data that I used for my code example (which the OP had provided in the first version of their post).

GoodFile.txt
Amimal, cat, 1, 4, YELLOW HAIR 3, HIGH 'this is a cattt 1 This section is the description of the animal bla bla lbas..... Amimal, dog, 2, 4, BLACK HEAD 1, HIGHf'this is a doggg 2 This section is the description of the animal bla bla lbas.....
BadFile.txt
Amimal, cat, 1, 4, YELLOW HAIR 3, HIGH 'this is a cattt 1 This section is the description of the animal bla bla lbas.....bla bla + lbas..... Amimal, cat, 3, 4, YELLOW LEG 3, HIGH 'this is a cattt 1 This section is the description of the animal bla bla lbas.....bla bla + lbas blaaaal........

Replies are listed 'Best First'.
Re^2: compare two files and print the differences of file 2 in a third file
by hopper (Novice) on Jun 15, 2017 at 20:57 UTC
    Thanks for taking time to take look at my code. I think you misunderstand my issues. The issues is that I have two files and each files has the multiple sections, and each section contents the description of it. The problem I am want to do is to compare two files and only print out the sections and subsection within that are not in file1 and save to the third file. Your edited code is comparing all the lines but I want to compare the sections and ignore the lines within the section. For example, file2: (Section, eighteen, 3, 4, YELLOW HAIR 3, HIGH, 'this is a cattt 1) has 7 strings) and if the line (7 strings) does match with the line in file 1 then I want to print out the no match information ( ex: Section, eighteen 3, 4, YELLOW HAIR 3, HIGH, 'this is a cattt 1, cccccccccccccc kkkkkkkkkkkkkk... So far, my code print out just (Section, eighteen 3, 4, YELLOW HAIR 3, HIGH, 'this is a cattt 1, ) it does not print out the subsections that belongs to the section, like cccccccccccccc.... . Please help me figure out what is the issues with my code. Thanks so much in advance.
      Hello lonnie,

      It appears that you edited your original post, and removed you original input data. This removed important context for some of the replies that you previously received. If you make an update to a post, it's less confusing if you add new information and keep the original information intact. You can do that by adding an update to the bottom of you original post. It makes it easier to identify an update if you annotate it in some way. For example,

      UPDATE: This is an update.

      Also, your original post now lacks proper formatting. Please use code tags like you did in your original post. If the format of your post is easier to read, you will likely receive more help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192754]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found