Suppose I have a file input.txt
with few columns and few rows, the first column is the key, and a directory dir
with files which contain some of these
First thing you should do is research this.
Next ... you don't need to grep inside awk. That's completely redundant. It's like ... stuffing your turkey with .. a turkey.
Awk can process input and do "grep" like things itself, without the need to launch the grep command. But you don't even need to do this. Adapting your first example:
awk '{print $1}' input.txt | xargs -n 1 -I % grep % dir
This uses xargs' -I
option to put xargs' input into a different place on the command line it runs. In FreeBSD or OSX, you would use a -J
option instead.
But I prefer your for loop idea, converted into a while loop:
while read key junk; do grep -rn "$key" dir ; done < input.txt