Matching column then append to existing File as new column

Good evening

I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this.

I have file called file1 and file2.

I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd column which is 300 in file 1 as 4th column.

Sample file1
ID KEY Time
01 text1_random_alphabets  400
02 text2_random_alphabets2 350
03 text3_random_alphabets3  1000

Sample file2:
ID KEY Time
01 text1_random_alphabets  300
02 text2_random_alphabets2 250
03 text3_random_alphabets3  400
09 textn_random_alphabets33  400


Desired Output

01 text1_random_alphabets  400 300
02 text2_random_alphabets2 350 250
03 text3_random_alphabets3  1000 400

I have read the file1 and in a loop and then for each line grep it in file2 then create a new file with 3rd column. But that seems to be lot of parsing and number of execution would be more as files contains more than 250000 records.

I am sure it would be better solutions than I have, Is there any other solutions/approach which I should be implement?. Appreciate your time.

Try:

awk 'NR==FNR&&NR>1{A[$2]=$NF;next};($2 in A){print $0,A[$2]}' file2 file1
1 Like

Excellent. It works beautifully :slight_smile:

Thank you

Could you please help me to understand this code.

awk 'NR==FNR&&NR>1  # Whilst the condition NR==FNR is true (which it will only be for the first file being read) and NR is greater than 1 
{A[$2]=$NF;next};   # Build an array where the index is the 2nd field and the value is the last field, stop processing and go to the next record.
($2 in A)           # If the 2nd field being read is present in the array called A
{print $0,A[$2]}'   # Print the line ($0), field seperator (,) and the value of A[$2]
file2 file1         # Read file2 first and then file1
1 Like

Thank you