Hi all,

I have two files namely
a.txt
b.txt
a.txt contains 100 records and b.txt contains 9 records.These 9 records in b.txt is also present in a.txt.So,I need a script that compares these two files and outputs the unique 91 records alone in another file.

Thanks to all in advance!!!!

Regards
dave.

Also am trying this in microsoft Access.

What do you mean by this? This forum is for Linux/Unix Shell Scripting, not Windows Batching (unless things have changed).

As far as doing the comparisons, an easy, but not necessarily performant way is a simple grep command.

a's got 100
b's got 9
you need the 91 uniques - I'll put 'em in c.txt

while read line
do
   grep $line b.txt >/dev/null 2>&1               
   if [ $? -ne 0 ]
   then
       echo $line >>c.txt
   fi
done <a.txt

Sorry - just needed to drop a post somewhere - it's been a while :)

, Mike

a's got 100
b's got 9
you need the 91 uniques - I'll put 'em in c.txt

while read line
do
   grep $line b.txt >/dev/null 2>&1               
   if [ $? -ne 0 ]
   then
       echo $line >>c.txt
   fi
done <a.txt

Sorry - just needed to drop a post somewhere - it's been a while :)

, Mike

Its actually easier, if with a few more file operations.

cp b.txt /tmp/b1.txt
while read line
do
   grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null
   mv -f b2.txt b1.txt
done <a.txt
mv b1.txt c.txt

Masijade,

I finished the file comparison in MS Access.But I am trying to do the same file comparison using Shell script...Thats why posted you a thread..

I didn't understand this line in your script :
grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null

Can you please explain?

Thanks in advance
Regards
dave.....

a's got 100
b's got 9
you need the 91 uniques - I'll put 'em in c.txt

while read line
do
   grep $line b.txt >/dev/null 2>&1               
   if [ $? -ne 0 ]
   then
       echo $line >>c.txt
   fi
done <a.txt

Sorry - just needed to drop a post somewhere - it's been a while :)

, Mike

Mike,

why are you using ">/dev/null 2>&1" in your script?
What condition you are checking in your "if" loop?

Regards
Dave

Masijade,

I finished the file comparison in MS Access.But I am trying to do the same file comparison using Shell script...Thats why posted you a thread..

I didn't understand this line in your script :
grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null

Can you please explain?

Thanks in advance
Regards
dave.....

grep -v means to match everything except the next text (you should probably place $line in quotes) from the the first file listed (b1.txt) and output it to the second file listed ( > b2.txt) and ignore any error messages (2>/dev/null).

The next line then moves b2.txt to b1.txt so you can repeat the procedure with the next line. Once you have gone through all lines, b2.txt contains all lines from b that were not in a.

Hey Dave,

I'm using ">/dev/null 2>&1" after grep in my script so no output will go to the screen from the grep command.

In the if loop, I'm checking the value of $? "errno - or the return code" - grep returns 0 if it's a success. The check needs to come right after the grep command is executed, because the $? variable gets populated with the return code (or error number) of whatever command was run last.

Hope that helps explain :)

, Mike

Mike,

why are you using ">/dev/null 2>&1" in your script?
What condition you are checking in your "if" loop?

Regards
Dave

Just used one command in script

diff a.txt b.txt | uniq > c.txt

We get the unique different records in c.txt

This article has been dead for over six months. Start a new discussion instead.