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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Need code for comparing
by ikegami (Patriarch) on Nov 04, 2009 at 15:36 UTC
Re: Need code for comparing
by bichonfrise74 (Vicar) on Nov 04, 2009 at 16:09 UTC
    Seems pretty straight forward. What have you tried so far? Hopefully, here is something to help you.
    #!/usr/bin/perl use strict; my $file_1 = <<HEREDOC; daw2n 189445 eastparkway ca daw1n 189445 eastparkway ca dawn 189445 eastparkway ca HEREDOC open( my $file, '<', \$file_1 ) or die "Cannot open $file_1"; my @first_records = <$file>; close( $file ); while (my $line = <DATA>) { for my $i (@first_records) { print "Record in file 1 found in file 2: $i" if ( $line eq "$i" ); } } __DATA__ kathy 329390 westbrook wa kat1hy 329390 westbrook wa dawn 189445 eastparkway ca kat2hy 329390 westbrook wa
Re: Need code for comparing
by graff (Chancellor) on Nov 05, 2009 at 07:58 UTC
    When you said:

    first value of file1 should compare should through entire first column of file2.

    Did you mean something like this:

    Print lines in file 1 where the first word on the line is not found in file2; also print lines in file 2 where the first word on the line is not found in file1.

    Or did you mean something different? You might want to look at a tool I posted here a few years ago: cmpcol. If I understand your task, you would get the results you want running that script like this:

    cmpcol -x file1 file2 > file.diffs
    For lines where the initial column is unique (eXclusive) to one input file or the other, the value of the first column will be printed to STDOUT (which you can redirect to a third file). The output indicates which input file ("1" or "2") each unique value came from.

    If you want to see the whole content of each line from file1 that begins with a name not found in file2, run it like this:

    cmpcol -x1 -l1 file1 file2 > in-file1-only
    and you can do likewise for lines unique to file2:
    cmpcol -x2 -l2 file1 file2 > in-file2-only
    You can also output the "intersection" (-i) or the "union" (-u) for the two files, instead of the "exclusive" (-x) content.