Results 1 to 4 of 4

Thread: awk - need help comparing 2 files

  1. #1
    Join Date
    Nov 2013
    Beans
    1

    awk - need help comparing 2 files

    My question is related to another previous thread (http://ubuntuforums.org/showthread.php?t=1520823) . I am having a similar situation. What I need is if col1 & 2 of file 1 matches col 1 & 2 of file 2, print that line of file 2.

    File 1:
    a 1 A
    a 2 T
    a 3 C


    File 2:
    a 2 Y
    a 3 R
    a 4 Q
    b 5 R


    output:
    a 4 Q
    b 5 R

  2. #2
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,050
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: awk - need help comparing 2 files

    Could it be as simple as:

    Code:
    awk '
    NR==FNR { 
        a[$1" "$2]=$3 
    } 
    NR>FNR { 
        k=$2" "$1; 
        if (k in a) 
            print k,a[k]; 
        else 
            print $0
    }
    ' file2 file1
    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

  3. #3
    Join Date
    Apr 2012
    Beans
    6,809

    Re: awk - need help comparing 2 files

    The output you've shown seems to be the complement of what your description says

    Another option might be to use grep -f, after cutting the 3rd column from file1, i.e.

    Code:
    $ grep -vf <(cut -d' ' -f-2 file1) file2
    a 4 Q
    b 5 R
    Code:
    $ grep -f <(cut -d' ' -f-2 file1) file2
    a 2 Y
    a 3 R
    Code:
    $ awk 'NR==FNR {a[$1" "$2]=$3; next} !a[$1" "$2] {print}' file1 file2
    a 4 Q
    b 5 R
    Code:
    $ awk 'NR==FNR {a[$1" "$2]=$3; next} a[$1" "$2] {print}' file1 file2
    a 2 Y
    a 3 R

  4. #4
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: awk - need help comparing 2 files

    Edit: Nevermind....got a bug. Gotta work on this for a minute more. Besides, Steeldriver's example is much, much better anyway!

    Edit: OK, bug fixed. Had to take another approach, although I can't quite figure out why. Steeldriver's solution is still a little better as it's a little less work. But, since I had a personal vendetta against this problem, here's my solution anyway:

    Code:
    #!/usr/bin/perl
    
    
    use warnings;
    use strict;
    
    
    my $file1 = shift;
    open( my $fh1, "<", $file1 ) || die "Can't open '$file1' for reading: $!";
    chomp( my @data1 = <$fh1> );
    close( $fh1 );
    
    
    my $file2 = shift;
    open( my $fh2, "<", $file2 ) || die "Can't open '$file2' for reading: $!";
    chomp( my @data2 = <$fh2> );
    close( $fh2 );
    
    
    my @results;
    for ( @data2 ) {
        my ($match) = $_ =~ /^(\w\s+\d)/;
        if ( grep { /$match/ } @data1 ) {
                print  "$_\n";
        }
    }
    Assuming the data size is small, it's easy enough to load them up into two arrays and then compare. My output is:

    Code:
    $ perl compare.pl file1.txt file2.txt 
    a 2 Y
    a 3 R
    Last edited by drmrgd; November 2nd, 2013 at 03:43 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •