merge based on common, awk help

All,

$ cat x.txt
z 11 az
x 12 ax
y 13 ay

$ cat y.txt
ay TT
ax NN

Output required:
y 13 ay TT
x 12 ax NN
>cat x.txt
z 11 az
x 12 ax
y 13 ay
>cat y.txt
ay TT
ax NN
>sort x.txt > sx.tmp && sort y.txt > sy.tmp && join -1 3 -2 1 -o "1.1 1.2 1.3 2.2" sx.tmp sy.tmp | sort -r && rm -fr *.tmp
y 13 ay TT
x 12 ax NN

it's no need to use awk! using join is ok, :slight_smile: and how to use awk?

.Aaron

But you're using join, 3 times sort and 2 temporary files!

With awk:

awk 'NR==FNR{a[$1]=$2;next}$3 in a{print $0 a[$1]" "a[$3]}' y.txt x.txt

Regards

If order matters:

awk 'NR==FNR{x[$3]=$0;next}$1 in x&&$0=x[$1] FS$2' x.txt y.txt

Use nawk or /usr/xpg4/bin/awk on Solaris.