comparing 2 files - kindly assist expert

Hi,

I'm new in shell scripting and would like u guys to help on this.

OS: Solaris 9

currently, i have 2 files.

file1:
hello
hi
test
god

file2:
hi
hello

file1 is my masterfile, and i wanted to print out string that are in file1 and NOT in file2 and send out a mail (with the line that is not appearing in file1) if there is a difference.

kindly assist

thanks a million

What have you done to attempt to solve this problem yourself?
Post your sample script, and we'll see how we can assist.

Regards

hi, i'm currently using the diff command but seems like it's not displaying what i wanted.

thanks

do want the files to be as such or we can sort the file for comparison and find out the difference ?

hi, sorting can also be done.. but i would like the file to be as it is if possible.

thanks

If you want to avoid sorting do the following:

  1. read your master file line by line setting up a loop
  2. in the loop do a "grep" on the second file with the line read
  3. If this proves unsuccessful print the value of the master file (or send it by mail, ...)

I hope this helps.

bakunin

Try this:

awk 'NR==FNR{arr[$0]=$0;next}!arr[$0]' file2 file1

Regards

Hi.

If you wish ..You can use the perl script to do the same .

#!/usr/bin/perl

$testfile1="<path of the 1st file>";
$testfile2="<path of the 2nd file>";
$outfile="<path of the output file you want to write into>"

open (OUTPUTFILE, ">>$outfile") or die ("File not found.");
open (FILE1, "$testfile1") or die ("File not found.");
@file1_contents=<FILE1>;

open (FILE2, "$testfile2") or die ("file not found.");
@file2_contents=<FILE2>;

my %seen;
@seen{@file2_contents} = ();

foreach $item (@file1_contents)
{
push (@miss,$item) unless exists $seen{$item};
}
foreach $rline (@miss)
{
chomp($rline);
printf OUTPUTFILE ("%s\n",$rline);
next;
}

close(FILE1);
close(FILE2);
close(OUTPUTFILE);

Regards

Antony cecil

sort a > a.t
sort b > b.t
comm a.t b.t
rm a.t b.t  

hi,

i have tried with nawk instead and it's working/

thanks all for the help!

franklin, do u have any good tutorial site where i can learn and understand the code u have given to me?

thanks

wish I can get franklin52 solution explanation.

I know basics in awk..but I got confused with arrays.

hope to understand the solution if possible.

thanks!

awk 'NR==FNR{arr[$0]=$0;next}!arr[$0]' file2 file1

NR==FNR{arr[$0]=$0;next} # NR == FNR is true if the first file is read, this line affects only the first file
{arr[$0]=$0;next} # set the array arr[$0] with $0 and read the next line
!arr[$0] # this is for the second file, if arr[$0] with the new $0 don't exist print $0

This is a good start to learn awk and sed:

sed & awk

And just like other things in life, you need to practice and learn from mistakes to understand the possibilities and know what to expect.

Regards

Hi guys,

how do i make it so that it will not compare for case (upper,lower case).

example:

file1: hello
file2: HELLO

will not filter any records if case is different.

thanks

sdif file1 file2 | grep '<' | mailx -s "Lines only in file1 but not in file2" <uremail -id>

Regards,
Ramkrix