How to run grep inside awk?

后端 未结 6 2141
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-15 12:11

    Use process substitution to create a keyword "file" that you can pass to grep via the -f option:

    grep -f <(awk '{print $1}' input.txt) dir/*
    

    This will search each file in dir for lines containing keywords printed by the awk command. It's equivalent to

    awk '{print $1}' input.txt > tmp.txt
    grep -f tmp.txt dir/*
    

提交回复
热议问题