Home > OS >  Print values from both files using awk as vlookup
Print values from both files using awk as vlookup

Time:03-25

I have two files, file1:

1 I0626_all 0 0 1 1 
2 I0627_all 0 0 2 1 
3 I1137_all_published 0 0 1 1 
4 I1859_all 0 0 2 1 
5 I2497_all 0 0 2 1 
6 I2731_all 0 0 1 1 
7 I4451_all 0 0 1 1 
8 I0626 0 0 1 1 
9 I0627 0 0 2 1 
10 I0944 0 0 2 1 

and file 2:

I0626_all 1 138
I0627_all 1 139
I1137_all_published 1 364
I4089 1 365
AfontovaGora2.SG 1 377
AfontovaGora3_d 1 378

At the end I want

1 I0626_all 138 
2 I0627_all 139
3 I1137_all_published 364

I tried using:

awk 'NR==FNR{a[$1]=$2;next} {b[$3]} {print $1,$2,b[$3]}' file2 file1

But It doesnt work.

CodePudding user response:

You may use this awk:

awk 'NR == FNR {map[$1] = $NF; next} $2 in map {print $1, $2, map[$2]}' file2 file1

1 I0626_all 138
2 I0627_all 139
3 I1137_all_published 364
  • Related