I am trying to take a file containing a list and count how many times items in that list occur in a target file. something like:
list.txt
blonde
red
black
target.txt
bob blonde male
sam blonde female
desired_output.txt
blonde 2
red 0
black 0
I have coopted the following code to get the values that are present in target.txt:
awk '{count[$2]++} END {for (word in count) print word, count[word]}' target.txt
But the output does not include the desired items that are in the liist.txt but not the target.txt
current_output.txt
blonde 2
I have tried a few things to get this working including:
awk '{word[$1]++;next;count[$2]++} END {for (word in count) print word, count[word]}' list.txt target.txt
However, I have had no success.
Could anyone help me make it so that this awk statement reads the key.txt file? any explanation of the code would also be much appreciated. Thanks!