awk with multiple files

I have 2 files

first is category:
1:Phone
2:Keyboard
3:Printer
4:Scanner
5:Mouse

the second is product:
1:iphone:1:.....
2:blackberry:1:.....
3:Mitsumi:2:.....
4:abc:5:.....
5:def:4:.....
6:noi:3:.....

which bold numbers are "foreign key" in product file and they are "primary key" of category file

Now, the user want to search product by category (they enter category name and i need return product in that category search in product file), i did:

keyword=`awk -F ":" ' $2~/'"${userkeywordinput}"'/ { print $1 }' category`

awk -F ":" '$3~'"${keyword}"' { print $0 }' product`

search successfull but the results were duplicated

Please help me to get the right result :slight_smile:
Thank you.

Try something like this:

awk -F ":" -v var="Phone" 'NR==FNR && $2==var {s=$1;next} $3==s ' file1 file2

Regards

i replaced var="Phone" to var=${userkeywordinput}
sorry but it not work, it just print two files category and product to the result

i want if the user input Scanner and the result is all product that have column 3 is 4, because in category file the id of scanner is 4

anyway, many thanks Franklin52

anyone help me, i really need it
thanks

Please don't bump up questions, that's agains the rules.

There was something wrong with the first solution (not tested), it should be something like:

userkeywordinput="Scanner"

awk -F ":" -v var="${userkeywordinput}" 'NR==FNR{if($2==var){s=$1}next}$3==s' file1 file2

Regards

thank you
it worked

sorry about bump thread