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

Hello all,

I know this is probrably easy. But since I am barely learning right now, I am in need of some help. I would like to compare two .txt files that is only one row of data in each. If the comparision is a match I need it to wirte to another .txt file. There is probrably more functions that I could do with this but this is my current issue.

Thanks is advance. Scott

Replies are listed 'Best First'.
Re: Comparison between files and report
by GhodMode (Pilgrim) on Feb 04, 2006 at 00:23 UTC
    Your needs seem pretty clear to me.
    my $file1 = "file1.txt"; my $file2 = "file2.txt"; my $file3 = "output.txt"; my $match = 0; # Open the files for reading open( FILE1, $file1 ); open( FILE2, $file2 ); while ( <FILE1> ) { my $string1 = $_; my $string2 = <FILE2>; print "$counter: $string1\n$string2\n\n\n"; ++$match if ( $string1 eq $string2 ); } close FILE1; close FILE2; if ( $match ) { open( FILE3, '> $file3' ); print FILE3 "$match lines matched\n"; close FILE3; }
    --
    -- GhodMode
    
      Thank you,

      That is what I needed.

      Scott
Re: Comparison between files and report
by Tanktalus (Canon) on Feb 03, 2006 at 23:43 UTC

    It may be easy ... but the hard part is figuring out what you mean. I don't know what you mean by "data", nor what the definition of "match" is.

    Can you show us the code you have so far, the sample data (both in match and not-match scenarios), and the expected output?

    Perhaps all you want to do is a shell script that calls "cmp", and copies the file to the output file if cmp return 0 (exactly the same contents). But I'm not sure.

Re: Comparison between files and report
by GrandFather (Saint) on Feb 04, 2006 at 00:18 UTC

    I wrote a meditation just for you. Read it to learn something about techniques for asking questions and providing a little code to show others where you are having a problem, then post you question again.

    Note that when you do so you may update your original node (but avoid removing the original text), or you may create a new node.

    The answer to your question may be as simple as slurp the files into two variables and cmp them, or it may be much more complicated. At this point we just don't know!


    DWIM is Perl's answer to Gödel
Re: Comparison between files and report
by smokemachine (Hermit) on Feb 04, 2006 at 03:28 UTC
    perl -e 'open(ARQ1, "<arq1");open(ARQ2, "<arq2"); open(OUT, ">outputfi +le");$arq.=$_ while <ARQ1>; $arq=~s/\n/|/g; while(<ARQ2>){print OUT u +nless /^($arq)$/}'