Lookup on large file based on a temp file

hello guys

Please help me with the below issue

I have two files one base file another lookupfile

base file

 
abc-001
bcd-001
cde-001

Lookupfile

 
abc-001|11|12
abc-001|11|12
abc-001|11|12
bcd-001|11|12
bcd-001|11|12
cde-001|11|12
cde-001|11|12
cde-001|11|12
cde-001|11|12

based on the base file value (like abc-001,bcd-001) I have to lookup the lookupfile and output the count for each base value

o/p

 
abc-001|3
bcd-001|2
cde-001|4

I think taking the values in base file into a variable and grep that value in the lookup file will work but couldnt find the right code

Thanks a lot for your valuable time and help

How about this?

awk -F"|" 'NR==FNR{a[$1]++;next}
a[$1]{print $1,a[$1]}' OFS="|" mainFile baseFile
1 Like

Thanks a lot Pravin

Need one more suggestion ,the value I am searching in lookup file is in column 4
So please let me know the modified code for this

 
Q|W|R|abc-001|11|12
Q|W|R|abc-001|11|12
Q|W|R|abc-001|11|12
Q|W|R|bcd-001|11|12
Q|W|R|bcd-001|11|12
Q|W|R|cde-001|11|12
Q|W|R|cde-001|11|12
Q|W|R|cde-001|11|12
Q|W|R|cde-001|11|12

Thanks again for your support

# awk -F'|' 'FNR==NR{a[$1]}($4 in a){A[$4,x]++}END{for(i in A)print i FS A}' base lookup
abc-001|3
bcd-001|2
cde-001|4
1 Like

Thanks a lot ...working perfectly:)

Hello ygemici ,

Thanks a lot for good post. Can you please explain the code as you used multidim array here. This will be use ful for us in future.

 
# awk -F'|' 'FNR==NR{a[$1]}($4 in a){A[$4,x]++}END{for(i in A)print i FS A}' base lookup

Thanks
Krsnadasa

i only use it (x or any) for the sequential print with index where in the (i in A)
you can read the usefull link :b:
Multi-dimensional - The GNU Awk User's Guide

regards
ygemici