Compare 2 files

HI

I have a list of items in 2 files. Some items are present in both the files in any order. Say for example item name is 'apple', it may be present in line 2 of file 1 and line 10 of file 2.

I want to see the missing items only. In simple terms, Items present in file1 if not in file2 then output the missing items to another file as "<itemname> not in file2.

thanks

I think that it works:

#!/bin/ksh
while read line
do
   echo $line
   grep -c $line $mydatafile2
   if [ $? -ne 0 ]; then
       echo "$line not in file2"
   fi
done < $mydatafile1

Always give a good exmple of your input and the output expected.
You will get response quickly.

Try this:

join -v 1 file1 file2

Assume both the files will be sorted, else try this:

sort -u file1 > file1x && sort -u file2 > file2x && join -v 1 file1x file2x

Good!!

[32]/home/t690177> cat arq1 
1 
2 
3 
[32]/home/t690177> cat arq2 
2 
[32]/home/t690177> join -v 1 arq1 arq2
1 
3 
[32]/home/t690177>

Two ways in perl:

file1:

apples
pears
oranges
file2:

apples
m1.pl

#!/usr/bin/perl
while (<>) {
    chomp;
    if ($ARGV=~/file2/) {
        $keys{$_}++;
    } else {
        print "$_\n" if (! $keys {$_});
    }
}

$ ./m1.pl file2 file1
pears
oranges
m2.pl

#!/usr/bin/perl

open FILE, "< $ARGV[1]" or die "Can't find $ARGV[1]: $!";
while (<FILE>) {
    chomp;
    $keys{$_}++;
}
close FILE;

open FILE, "< $ARGV[0]" or die "Can't find $ARGV[0]: $!";
while (<FILE>) {
    chomp;
    print "$_\n" if (! $keys {$_});
}
close FILE;

$ ./m2.pl file1 file2
pears
oranges

Personally, I prefer methods like these over sort because they can process millions and millions of records in both files very fast passing through each set only once.

grep -vif file1 file2 > new_file