How to use grep to find the string from the pipeline |


Today, we have a problem and need to view the business log. The scenario is like this: each request will generate multiple lines of business log. The first line of log records the request parameter uid, and each line of business log will have a traceid. First, we need to find the business log of all requests of a person based on his uid.

For example:
2020/05/26 23:26:40.041 [I] [xxx.go:34]  br6jac5jeehi2459hk0g RequestParam: {"uid":213123123} 
2020/05/26 23:26:40.126 [I] [bbb.go:53]  asdasdasdasdasdasdas Response CostTime: 84.806814ms 
2020/05/26 23:26:40.041 [I] [ccc.go:34]  sdfasdfasdfasfasdfas RequestParam: {"uid":123123}
2020/05/26 23:26:40.041 [I] [sss.go:61]  br6jac5jeehi2459hk00 Abtest Api:
2020/05/26 23:26:40.043 [I] [fff.go:84]  br6jac5jeehi2459hk00 Abtest Resp: {}

The fifth column is traceid
The logical order is that grep takes out all traceids of this person’s uid, and takes out all the relevant traceid lines

Start operation:
Method 1

As an awk expert, there is nothing awk can’t handle

grep '213123123' /info.log |awk '{b=system("grep --color "$5" /info.log");print b}'

The result is feasible, but the only regret is that the system function will return the result of the command execution, so there are a series of strange 0, 1, 0, 1, obsessive-compulsive disorder can not stand.

Method 2

Pipeline with xargs plus – I parameter means to replace each line of the previous pipeline output as a parameter to the position where {} is located. The problem is solved perfectly.

grep '213123123' /info.log |awk '{print $5}'|xargs -i grep {} test.txt

If you don’t want to use {} as a replacement place holder, – I can specify a place holder, so the above can be written as well

//Specify! As a placeholder
grep '213123123' /info.log |awk '{print $5}'|xargs -I ! grep ! test.txt

But – I is not supported in MAC, only – I is supported