0

I know there are several similar questions about there, but I can't get the answers to work and I am completely flummoxed as to what i'm doing wrong.

I have a file that looks like:

 name1 na 1002
 name2 na 1002
 name3 na 1003

I want to query it with a key file that looks like:

 1002 1
 1003 2

desired output:

 name1 na 1002 1
 name2 na 1002 1
 name3 na 1003 2

However, i'm only getting the output of the first file using this code:

 awk  'FNR==NR {a[$1]=$2;next} $3 in a {print $0,a[$1]}' key file

what am I missing that it's not printing anything for the a[$1]?

Ascaris
  • 83
  • 7
  • Are the leading spaces part of the files? – Cyrus Mar 13 '23 at 15:18
  • Does this answer your question? [Inner join on two text files](https://stackoverflow.com/questions/13272717/inner-join-on-two-text-files) – tink Mar 13 '23 at 16:47
  • 2
    Or [this](https://stackoverflow.com/questions/49296740/how-to-join-two-files-based-on-their-second-column-without-changing-the-order-of) or any of the other 100+ duplicates ;) – tink Mar 13 '23 at 16:51
  • There's no `1002 1` in the first file. Do you want to do the lookup only with parts of the entries from the key file? And you want the output to replace the key from the first file with the full entry from the key file? – Cristik Mar 14 '23 at 06:40

1 Answers1

1

You need to print a[$3] instead of a[$1] as the value that was in the 1st field in your first file, key, (e.g. 1002) is in the 3rd field in your 2nd file, file, as you are already testing with $3 in a.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185