Friday, November 16, 2018

Vlookup equivalent in awk: Reader's request

Objective:Print the value of field 1 and 3 from file b.txt based on the content of field 4 in a.txt

a.txt1,2,3,DELHI,4,5
4,2,1,MUMBAI,8,2
12,11,54,PUNE,11,2
7,12,8,GOA,9,3
9,2,1,BANGALORE,2,3

b.txtPUNE,13,10000
MUMBAI,100,20000
GOA,9,4000
NOIDA,43,9000
DELHI,7,3000
GURGAON,8,800

Solution:awk -F ',' 'FNR == NR {array[$4]; next} {if ($1 in array) print $1 "," $3}' a.txt b.txt

Example:[user@server ~]$ awk -F ',' 'FNR == NR {array[$4]; next} {if ($1 in array) print $1 "," $3}' a.txt b.txt
PUNE,10000
MUMBAI,20000
GOA,4000
DELHI,3000
 

Blogger news

Blogroll