Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

compare two text files

by juergenkemeter (Novice)
on Jul 14, 2005 at 04:53 UTC ( #474745=perlquestion: print w/replies, xml ) Need Help??
juergenkemeter has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have two Test files *.txt. The first of them contains general data written in a column. The second textfile there are several search strings written in a column. How do I compare these two files so that the output would tell me which of the search strings can be found in the general data column of the first textfiles? This posting is quite similar to my former one which is concerning the comparison of two Excel Spreadsheets. Thanks, Jurgen

Replies are listed 'Best First'.
Re: compare two text files
by prasadbabu (Prior) on Jul 14, 2005 at 05:02 UTC

    If i understood your question correctly this should work for you. This can be done still efficiently.

    open (GEN, "<general.txt") || die ("cannot open general.txt"); open (SEA, "<search.txt") || die ("cannot open search.txt"); undef $/; $gen = <GEN>; $sea = <SEA>; @gen = split /\n/, $gen; @sea = split /\n/, $sea; for $a (@gen) { @result = grep/^\Q$a\E$/, @sea; push (@final , @result); } print "Search string that matches against general data:\t@final";


      Hi,thanks for your code. I tested it and it seems that it only works for whole strings. Can it be changed so that it also searches for substrings in the 'general.txt' - file? And since I am a beginner, a few explaining words about your code would be very helpful, thanks! Jurgen

        You just remove the anchors ^ and $ to match the substrings.

        @result = grep/\Q$a\E/, @sea;


Re: compare two text files
by atcroft (Monsignor) on Jul 14, 2005 at 05:13 UTC

    First of all, while I don't know in this case if this is homework, the way your post is written sounds like some homework-like questions I have seen before. Most people here have already done their share of homework, and so may be a little reluctant to do someone else's as well. Show what you've tried so far, or even how you think you might, and you will likely get more/better results in the future. That having been said, now back to our regularly-scheduled post.

    If the search list is short enough, you can store the items as keys in a hash, then loop thru general data file and push the line numbers into the hash when found, and when done list the entries found. Something like the following (untested) code might work...

    open(SF, $searchfile) or die(qq{Can't open $searchfile for input: $!\n}); while (<SF>) { chomp; @{$results{$_}} = (); } close(SF); open(DF, $datafile) or die(qq{Can't open $datafile for input: $!\n}); while (<DF>) { foreach my $k (keys %results) { if (m/$k/) { push(@{$results{$k}}, $.); last; } } } close(DF); foreach my $k (sort keys %results) { if (scalar @{$results{$k}}) { printf "%s:\n\t%s\n", $k, join(",", sort @{$results{$k}}); } }

    Hope that helps.

Re: compare two text files
by Samy_rio (Vicar) on Jul 14, 2005 at 05:16 UTC


    use List::Compare; use strict; open (F, "a.txt")||die("a.txt File cannot open\n"); open (S, "b.txt")||die("b.txt File cannot open\n"); my @a=<F>; my @b=<S>; my $lcma = List::Compare->new(\@a, \@b); print $lcma->get_complement ,"\n"; # extra present in the second array + print $lcma->get_unique ,"\n"; # extra present in the First array

    I think it helps you.

    Velusamy R.

      Hi! It all works fine, thank you. Next time I will post my "trying code" with my thread. In case of homework: I ordered the book "Learning Perl" today. One last question occured to me though: is there a way to put the output into a new textfile? Thanks a lot for your fast help, Jurgen


        open(OUT, ">new.txt")||die("cannot create\n"); print OUT "\nExtra Text present in the second File\n", $lcma->get_comp +lement ,"\n"; print OUT "\nExtra Text present in the First File\n", $lcma->get_uniqu +e ,"\n";

        You just replace the last two line of my code with the above code.

        Velusamy R.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://474745]
Approved by jbrugger
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-03-30 18:42 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (362 votes). Check out past polls.