Operating System - HP-UX
1752579 Members
4630 Online
108788 Solutions
New Discussion юеВ

Uregent : Record Comparison Between 3 files

 
Balasubramanian
Occasional Contributor

Uregent : Record Comparison Between 3 files

Hi

I need to compare 3 files in this manner.

First i will take file1 and compare each and every record of file1 in terms of field with other 2 files. Based on process rules, i should do some action.

My question is how i can do record comparison among files ? I am trying to do this with Shell Scripting, AWK.


Could some one tell me the how can i
achive it ?


Awaiting your early reply.



Thanks
Bala
12 REPLIES 12
Vikas Khator
Honored Contributor

Re: Uregent : Record Comparison Between 3 files

Hi ,

Look at the comm command
Keep it simple
James R. Ferguson
Acclaimed Contributor

Re: Uregent : Record Comparison Between 3 files

Hi:

Take a look at the man pages for 'diff' and 'cmp'.

...JRF...
Jacques Simon
Advisor

Re: Uregent : Record Comparison Between 3 files

What about diff?! (see man diff)
Balasubramanian
Occasional Contributor

Re: Uregent : Record Comparison Between 3 files

Hi,

Thank you all for your reply.

Even if i am using 'comm' or diff command i can compare only 2 files. In my case i have to compare the entire record field by field from the first file with the other two files (again record field by field).


Awaiting ur reply.


Thanks
Bala
Curtis Larson
Trusted Contributor

Re: Uregent : Record Comparison Between 3 files

do your files all have the same number of records? do all the records have the same number of fields? What is your field seperator?

in the korn shell do something like this:

#save your IFS variable
OLDIFS=$IFS

#set IFS to your field seperator
IFS="," # comma seperated fields,
# probably want to add your
# record seperator here too

# open file descriptor to the files to be compared

exec 3< file1
exec 4< file2

cat yourfile |
while read field
do
read -u3 field1
read -u4 field2
if [[ $field != field1 ]] ;then
if [[ $field != field2 ]] ;then
do_case1 #!!
else
do_case2 #!=
fi
else
if [[ $field != field2 ]] ;then
do_case3 #=!
else
do_case4 #==
fi
fi
done

IFS=$OLDIFS

of course this assumes same number of records in each file and the same number of fields in each record.

Carlos Fernandez Riera
Honored Contributor

Re: Uregent : Record Comparison Between 3 files

Hi:

To acomplish this task I often use join command.

i.e:
sort f1 > f1.o
sort f2 > f2.o
sort f3 > f3.o


join -a 1 -a 2 -e "MISSING" -o 1.1 1.2 ... 2.1 2.2 file1.o file2.o > j1


join -a 1 -a 2 -e "MISSING" -o 1.1 1.2 ... 2.1 2.2 j1 file3.o > j2

And now is very easy to process with awk.


See man paste and join.


unsupported
Andre Lattier
New Member

Re: Uregent : Record Comparison Between 3 files

Hi,

in Xemacs editor, have a look at :
Tools->Compare->Three files...
Balasubramanian
Occasional Contributor

Re: Uregent : Record Comparison Between 3 files

Hi

Thanks every one for your reply.

Larson, my input files have the different no. of records and have the same no. of fields.
The field separator is pipe symbol (|).

I tried with this script,

Korn Shell
==========

OLDIFS="$IFS"
IFS=|
echo "Enter the first file"
read file1
echo "enter the second file"
read file2
exec 3<$file1
exec 4<$file2
exec 5>match
exec 6>nomatch
while read -u3 lineA
do
read -u4 lineB
if [ "$lineA" = "$lineB" ]
then
print -u5 "$lineA"
else
print -u6 "$lineA; $lineB"
fi
done
IFS=$OLDIFS


This scripts compare the records one after another from both the files. But what i need is,

i have to take a record from the first file (say file1) and compare that record with each and every record of the second file (say file2)
until EOF of file2. Similarly till EOF of file1.


Could you pl. anyone tell me how can i write the script to do this ?

I have a requirement to compare not only at the record level but i need to compare at the field level.

Awaiting your reply.


Thanks
Bala
Curtis Larson
Trusted Contributor

Re: Uregent : Record Comparison Between 3 files

not exactly elagant but should get the job done:

#read each line of your file
while read -u3 line
do
#test line against these files
for i in file1 file2
do
#get each line that is different + the line number
grep -nvx "$line" file1 |
while read dfline
do
#get the line number that is different
numRec=$(print "$dfline" | cut -d: -f1)
dfline=$(print "$dfline" | cut -d: -f2-)

numFields=$(print "$dfline" | awk -F\| '{print NF;}')

(( x = 0 ))
while (( $x < $numFields ))
do
(( x = $x + 1 ))
field=$(print "$line" | cut -d: -f"$x")
field1=$(print "$dfline" | cut -d: -f"$x")

if [[ "$field" != "$field1" ]] ;then
print "$file1:$numRec:$x:$field:$field1"
fi
done
done
done
done