Load the information in memory with awk
and then print the line if the id
matches:
$ awk 'FNR==NR {a[$1]=$2; next} ($1 in a) {print $1, $2, a[$1]}' f2 f1
1 a 002
2 b x90
Explanation
The basic idea is to do things
when reading file1
and other_things
when reading file2
:
awk 'FNR==NR {things; next} {other_things}' file1 file2
In our case, things
is to store the content of file2
in memory, mapping every id to its value.
Then, it goes through file1
and prints the content of the line, together with the mapped value, if there is a common id.