I want to write a script which will compare field(s) from two different files. The data in these fields is numeric and I want to identify the row number and data element where there is a match.
I have sketched out some pseudo-code which may help explain my question; my explanations are denoted by **
(1) Use the following:
awk '{print $<field# from file#1>}' file#1>f1
* put field # from file #1 into output file f1
awk '{print $<field# from file#2>}' file#2>f2
* put field # from file #2 into output file fw
awk 'compare_file_fields' f1 f2
* run the script below using files f1 and f2
BEGIN
for (a=1; a<=NF, a++)
x(a)={ print f1 $<field# from file#1>}
y(a)={ print f2 $<field# from file#2>}
* put field elements from file 1 and file 2 into variables x and y
{
for (i=1; i<=NF; i++)
{
for (j=1; j<=NF; j++)
{if (x(j)==y(i))
<print data element from x that matches y and also print the row number>
** check to see if any element from x matches any element from y and print out that element along with the row # in file 2 where the match occurred
}
}
}
What is the best way to accomplish this numeric comparison of field elements between two different files?
I have sketched out some pseudo-code which may help explain my question; my explanations are denoted by **
(1) Use the following:
awk '{print $<field# from file#1>}' file#1>f1
* put field # from file #1 into output file f1
awk '{print $<field# from file#2>}' file#2>f2
* put field # from file #2 into output file fw
awk 'compare_file_fields' f1 f2
* run the script below using files f1 and f2
BEGIN
for (a=1; a<=NF, a++)
x(a)={ print f1 $<field# from file#1>}
y(a)={ print f2 $<field# from file#2>}
* put field elements from file 1 and file 2 into variables x and y
{
for (i=1; i<=NF; i++)
{
for (j=1; j<=NF; j++)
{if (x(j)==y(i))
<print data element from x that matches y and also print the row number>
** check to see if any element from x matches any element from y and print out that element along with the row # in file 2 where the match occurred
}
}
}
What is the best way to accomplish this numeric comparison of field elements between two different files?