Compare two text files line by line

Hi all

I need help on comparing two texts files line by line and print the total number of lines matched. Any help is appreciated. Thanks in advance.

"a" and "b" are the names of the files to compare.

IFS=''
exec 3<a 4<b
while true; do
    read -ru3 a && read -ru4 b || break
    [ "$a" == "$b" ] && echo
done | wc -l

Should work with bash and ksh (and probably most modern bourne variants).

Regards,
alister

I am getting an error message

read: 9: Illegal option -u

Anything is missing ?

Depends on what you actually mean by "line by line"...

$ head file[12]
==> file1 <==
a
c
d
e
f

==> file2 <==
a
b
d
f
g
$ comm -12 file1 file2 | wc -l
       3
$ paste -d '~' file1 file2 | awk -F '~' '$1==$2' | wc -l
       2

---------- Post updated at 02:46 PM ---------- Previous update was at 02:36 PM ----------

Using comm, there are 3 matching lines, i.e. lines containing "a", "d" and "f".
Using awk, there are 2 matching lines,i.e. line 1: "a" and line 3: "d". Note that the two lines containing "f" are not counted since they are on different lines.

Thanks a lot. I am using awk one. It works.