Problem
Suppose you have a recipe text file called recipes.yml
Margherita:
cheese
tomato
Chicken Supreme:
cheese
onions
chicken
mushrooms
Veggie:
cheese
spinach
sweetcorn
peppers
mushrooms
onions
Potato:
cheese
potato
oregano
Now I would like to find any pizza that contains either cheese, onion or rucola. I will put my search terms into another file
$ cat terms.txt
cheese
onion
rucola
Desired output
$ while read -r line; do echo "searching pizza containing: $line" && SEARCH $line IN recipes.yml; done <terms.txt
searching pizza containing: cheese
found 4
Margherita
Chicken Supreme
Veggie
Potato
searching pizza containing: onion
found 2
Chicken Supreme
Veggie
searching pizza containing: rucola
found 0
Maybe this is too much to do in bash but I would really like to know if it is possible at all. I am stuck right now. I cant seem to find a way to capture the name of the pizza given the ingredient is found. Here are some half-way attempts using grep, awk and sed:
Attempts
I have only been able to find commands to let me find the number of occurrences of each search term and on what line the match is located in the file. Like this:
$ while read -r "line"; do echo "searching pizza containing: $line" && grep -c "$line" recipes.yml && grep -n "$line" recipes.yml; done <terms.txt
searching pizza containing: cheese
4
2: cheese
6: cheese
12: cheese
20: cheese
searching pizza containing: onion
2
7: onions
17: onions
searching pizza containing: rucola
0
and with awk and sed
$ while read -r "line"; do echo "searching pizza containing: $line" && awk -v avar="$line" '$0 ~ avar {count++} END {print count}' recipes.yml && sed -n "/$line/p" recipes.yml; done <terms.txt
searching pizza containing: cheese
4
cheese
cheese
cheese
cheese
searching pizza containing: onion
2
onions
onions
searching pizza containing: rucola
grapeas a search term withgrapefruitin the recipe to make sure there aren't false matches when testing.