2auri
2auri

Reputation: 3

awk to filter lines in a file based on match and conditional of another file

I have a file with this format:

file1

id1   12.4
id2   21.6
id4   17.3
id6   95.5
id7   328.6

And I want to filter it based on another file with the format:

file2

id1   11.5
id2   10.4
id3   58.4
id4   24.6
id5   234.4
id6   2.5
id7   330.6

First, I would like to match ids between files. Then, I want to keep the lines in file1 in which the score (second column) is greater than the score in file2. It would output this:

id1   12.4
id2   21.6
id6   95.5

I started writing the code like awk 'FNR==NR { a[$1][$2][$0]; next } $1 in a {}' file1 file2 which I think would match the ids between files, but I don't know how to complete the code to filter by the scores.

Upvotes: 1

Views: 176

Answers (1)

The fourth bird
The fourth bird

Reputation: 163642

You could write the awk command first reading file2, and then keep track of the values by setting the value a[$1] = 0+$2 and add zero for a numeric comparison

Then you can do the comparison with file1

awk 'FNR==NR { a[$1] = $2+0; next } $1 in a && $2+0 > a[$1]' file2 file1

Output

id1   12.4
id2   21.6
id6   95.5

Upvotes: 2

Related Questions