This is a time when you may want to use existing commands that do this sort of thing, at least to prime your Perl code. Two that come to mind on UNIX:

  1. The comm command does what you want if the files are sorted.
  2. You could prime the process with this:
    sort file1 file2 | uniq -u
    The output of that is just the lines that one has but the other doesn't. This means you only have to read one of the files if you do this sort of thing:
    local *IN; my %diffs; open(IN, "sort file1 file2 | uniq -u |") or die "Failed to open pipe to sort/uniq command: $!\n"; ++$diffs{$_} while (<IN>); close(IN) or die "Failed to close pipe: $!\n"; open(IN, "<file1") or die "Failed to open file1: $!\n"; print "In file1 only:\n"; while (<IN>) { if ($diffs{$_}) { print $_; delete $diffs{$_}; } } print "\nIn file2 only:\n", keys %diffs, "\n";


In reply to Re: Re: Re: Line by line file compare w/ low system memory by steves
in thread Line by line file compare w/ low system memory by zoot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.