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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing contents of new files - numerical example

Status
Not open for further replies.

AwkRookie

Programmer
Oct 15, 2002
10
US
marsd,

Sorry.

Here's my example:

file 1
------
contains fields file1_field1 thru file1_field4 with numeric data (double-precision) as:


file1_field1 file1_field2 file1_field3 file1_field4
16:00 10 20 30
16:10 40 50 60
16:20 12 23 14

file 2
------
contains fields file2_field1 thru file2_field4 with numeric data (double-precision) as:

file2_field1 file2_field2 file2_field3 file2_field4
16:00 10 22 31
16:10 20 50 60
16:20 14 29 13

Now, I want to see if row 1, field 3 from file #1 matches row 1, field 3 from file #2 OR row2, field 3 from file #2 OR rowN, field 3, from file #2 (where N is the number of rows in file 2). Once I check to see if row 1, field 3 from file #1 matches all of the corresponding elements in field 3 of file #2, I would index to row 2. Now, I check to see if row 2, field 3 from file #1 matches row 1, field 3 from file #2 OR row 2, field 3 from file #2, and so on.

The output should be:

row 1, field 2 match between files #1 and #2 at row number 1, field #2 (both have a value of 10)

row 2, field 2 match between files #1 and #2 at row number 2, field #2 (both have a value of 50)

and so on ...

output to screen would be:

value of 10 found in row 1
value of 50 found in row 2

Also, I would like to store the row#s and values of the solution in a matrix as follows; for each value in field #1 of:

16:00 10 1
16:10 50 2
16:20 no match

The size of the output file for this script should correspond to the size of file #2, i.e., for every row in file #2, there should either be a value & row number or the text "no match".

Let me know if this is a sufficient input/output description of what I am trying to do.

AwkRookie
 
I'm not sure if I understand exactly what you want, but try this.

awk -v fn="file1" -v fn3="file3" -f match.awk file2

# ------ match.awk ------
BEGIN {
if (!fn) fn = "file1"
if (!fn3) fn3 = "file3"
while ((getline < fn) > 0) {
a2[$2] = 1
a3[$3] = 1
}
}
{
if (a2[$2] || a3[$3]) {
if (a2[$2]) j=$2
else j = $3
print &quot;Value of &quot; j &quot; found in row &quot; NR
print $1 &quot; &quot; j &quot; &quot; NR > fn3
}
else {
print $1 &quot; no match&quot; > fn3
}
} CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top