How to run grep inside awk?

后端 未结 6 2140
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 11:22

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

6条回答
  •  太阳男子
    2021-02-15 11:47

    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
    

提交回复
热议问题