I'm sorry if this is extremely elementary, but I just can't figure out how to do this, and my research has failed me as well.I have two files: data.csv and list.txt. Here's an example of what they look likedata.csv:John,red,4Basketball,orange,2The Mike,blue,94Lizard,purple,3Johnny,pink,32list.txt:MikeJohnpurple32Now, I am trying to figure out how I can make a loopawk -F \*,\* '/**LIST ITEM**/ {print $1}' data.csv > output.txtwhere the command runs for each line of list.txt, replacing **LIST ITEM**. How can this be accomplished?I'm running this though Terminal on MacOSX 10.5.7.EDIT:The desired output for the above example would beThe MikeJohnJohnnyLizardJohnnyEDIT2:To be more clear, I am trying to avoid doing this:awk -F \*,\* '/Mike/ {print $1}' data.csvawk -F \*,\* '/John/ {print $1}' data.csvawk -F \*,\* '/purple/ {print $1}' data.csvawk -F \*,\* '/32/ {print $1}' data.csvAnd instead, run it in one command, somehow looping through all the lines of list.txt.
Loop a list through awk
awk
This meets the order of your desired output:$ awk -F, '    NR == FNR {field1[$0] = $1; next}    {      for (line in field1)         if (line ~ $0)           print field1[line]    }  ' data.csv list.txt The MikeJohnJohnnyLizardJohnnyThis reads the data.csv file into memory, mapping the whole line to field1. Then, each line of the list.txt file is checked against each element of the field1 array.If the data file is much larger than the list file, then it would make more sense to hold the smaller file in memory and loop over the larger file a line at a time:$ awk -F, '    NR == FNR {list[$1]; next}    {      for (item in list)         if ($0 ~ item)           print $1    }  ' list.txt data.csv JohnThe MikeLizardJohnnyJohnny