Monday, December 10, 2012

Print lines of file 1 which are not present in file 2. Also second field of file 2 may be different from file 1.


#!/usr/bin/perl -w
use strict;
use English qw(-no_match_vars);
die "Manadatory Parameter missing!Exiting..." if @ARGV < 2;
my ($file1, $file2) = @ARGV;
open my $file_handle1, "<", $file1 || die "Error opening $file1 - $OS_ERROR";
open my $file_handle2, "<", $file2 || die "Error opening $file2 - $OS_ERROR";
while (<$file_handle1>) {
    my $file1_line = $_;
    my $file1_id   = ($file1_line =~ /^(.+?)\;/) [0];
    my $match_found;
    while (<$file_handle2>) {
        my $file2_line = $_;
        my $file2_id   = ($file2_line =~ /^(.+?)\;/) [0];
        if ($file1_id == $file2_id) {
            $match_found = 1;
            last;
        }
    }
    seek $file_handle2, 0, 0;
    print $file1_line if ! $match_found;

}

2 comments:

Harpreet said...

Try:

comm -23

cheers :)

Manu Swami said...

Yes Harpreet, the comm would also work. But dies when the filesize is in Gigs :(

My intention was to make a PERL utility. :)

 

Blogger news

Blogroll