1
votes

Looking for an awk one liner:

I have two files each with two columns

I need to print records where the first column match in both files and where the second file value column is > than the first file value column.

file1

AAAA 322
BBBB 322
DDDD 200

file2

AAAA 700
CCCC 400
DDDD 100

Looking for result

AAAA 700

Appreciate any help. So far I can match the column 1 but not sure how to calculate the second value column when it's >

awk 'NR == FNR{a[$1];next}$1 in a' file1.txt file2.txt
AAAA 700
DDDD 100

updated

I think I may have gotten it by reversing the file order and using:

awk 'FNR==NR{a[$1]=$2 FS $3;next}{ print $0, a[$1]}' file2.txt file1.txt|awk '$3 > $2' |awk '{print $1" "$3}'
AAAA 700
2

2 Answers

1
votes

Input

[akshay@localhost tmp]$ cat file1
AAAA 322
BBBB 322
DDDD 200

[akshay@localhost tmp]$ cat file2
AAAA 700
CCCC 400
DDDD 100

Output

[akshay@localhost tmp]$ awk 'FNR==NR{A[$1] = $2; next}($1 in A) && $2 > A[$1]' file1 file2
AAAA 700
1
votes
awk 'FNR==NR{a[$1]=$2 FS $3;next}{ print $0, a[$1]}' file2.txt file1.txt|awk '$3 > $2' |awk '{print $1" "$3}'
AAAA 700

this achieved the output I was after