awk的结果作为grep的搜索模式

时间:2017-06-13 17:32:45

标签: bash awk grep

这是一段日志

21:36 b05808aa-c6ad-4d30-a334-198ff5726f7c new
22:21 59996d37-9008-4b3b-ab22-340955cb6019 new
21:12 2b41f358-ff6d-418c-a0d3-ac7151c03b78 new
12:36 7ac4995c-ff2c-4717-a2ac-e6870a5670f0 new

我用awk'{print $ 2}'st.log打印它 所以我得到了

b05808aa-c6ad-4d30-a334-198ff5726f7c
59996d37-9008-4b3b-ab22-340955cb6019
2b41f358-ff6d-418c-a0d3-ac7151c03b78
7ac4995c-ff2c-4717-a2ac-e6870a5670f0

现在我需要以这种方式将它传递给grep

awk '{print $2}' |xargs -i grep -w "pattern from awk" st.log

我需要确切地知道如何将每个创建的记录从awk传递给grep。我不需要其他解决方案,因为我的任务比这件事更复杂。谢谢。

3 个答案:

答案 0 :(得分:1)

使用bash和grep:

grep -f <(awk '{print $2}' piece_of_log) st.log

答案 1 :(得分:1)

不需要awk:

grep -Ff <(cut -d' ' -f2 log)

答案 2 :(得分:0)

您似乎正在寻找替换字符串选项:

   -I replace-str
          Replace occurrences of replace-str in the initial-arguments with
          names read from standard input.  Also, unquoted  blanks  do  not
          terminate  input  items;  instead  the  separator is the newline
          character.  Implies -x and -L 1.

像这样:

awk '{print $2}' | xargs -I{} grep -w {} st.log