How to use grep to find a pipeline

Posted May 27, 20202 min read

Cause

I encountered a problem today and need to check the business log. The scenario is probably like this. Each request will generate multiple lines of business logs. The first line of logs records the request parameter uid, and each line of business logs will have a traceid. We first need to find a business log of all requests by a person based on his uid.

E.g:
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]asdasdasdasdasdasdasdas 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:{}

Column 5 is traceid
The logical sequence is that grep takes out all traceid of this person's uid, and takes out all the lines with related traceid

Start operation:
method 1:

As a little awk expert, there is nothing that awk can't do

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

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

Method 2

The pipeline cooperates with xargs plus the -i parameter, which means that each line output of the previous pipeline is replaced as the parameter where {} is located, and the problem is solved perfectly.

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

If you do n t want to use {} as a placeholder, -I can specify a placeholder, so the above can also be written like this

//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