Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing the contents of two files

Status
Not open for further replies.

agilo

Programmer
Feb 4, 2004
73
TR
Hi,

I want to compare the contents of two text files using Awk. Both files are sorted. I want to see which values changed, deleted or newly added.
E.g.,

file1:

x1 x2 x3 x4 x5 x6
name1 4 5 6 8 10
name2 10 33 44 55 32
..................................
..................................

file2:

x1 x2 x3 x4 x5 x6 x7
name1 4 5 6 10 10 13
name2 10 33 44 45 12
.......................................
.......................................

The comparison should show that the value of x5 changed from 8 to 10, also x7 is a new column in the second file, the value of x6 for name2 in the second file has been deleted.

any ideas are welcomed..

Thanks,

Agilo
 
You can use the Unix function sdiff (side by side difference).

Sometimes the grass is greener on the other side because there is more manure there - original.
 
Agilo,

I am certain that there is a way to do this with awk, but have you considered using the diff command to do the file comparison?

The way I would approach doing what you desire with awk would be to read the lines from each of the files then do a field comparison checking for the values not being equal.

While I am sure this method would work there is a lot of overhead for reading and comparing the files this way.


Hope this helps,

Steve
 
Here's something to get you started. The spacing of the columns was different in the sample data you posted so I adjusted them to be 6 characters each. You may have to change the substr statements to reflect your actual data.

BEGIN {
if (!fn) fn="file3"
while ((getline < fn) > 0) a[++n] = $0
close(fn)
}
{
if (NR==1) {
n = split(a[1],b)
if (NF != n) print "different number of cols"
next
}
for (j=1;j<n;j++) {
a1 = substr($0,j*6,6)+0
a2 = substr(a[NR],j*6,6)+0
# print a1,a2
if (a1 != a2) print "your error message here"
}


CaKiwi
 
This uses an array to hold all the values, then does a comparison.
[tt]
awk 'BEGIN {
FS=" {1,9}"
}
FNR==1{
afn[++fc]=FILENAME
split($0,acn)
for(x in acn) {
cn=acn[x]
ucn[cn]=cn
}
next
}
{
for(x=2;x<=NF;x++) {
cn=acn[x]
av[FILENAME,$1,cn]=$x
urn[$1]=$1
}
}
END {
for(z in urn)
for(y in ucn) {
f1=afn[1]
f2=afn[2]
if (av[f1,z,y] != av[f2,z,y])
printf "%s %s\t %s:%6s\t %s:%6s\n",
z, y, f1, av[f1,z,y], f2, av[f2,z,y]
}
}' File1 File2
[/tt]
...tested on the sample data...
[tt]
name1 x5 File1: 8 File2: 10
name1 x7 File1: File2: 13
name2 x5 File1: 55 File2: 45
name2 x6 File1: 32 File2:
name2 x7 File1: File2: 12[/tt]
 
Thanks very much guys,

the script from Ygor does what I need..

Agilo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top