问题
I have below varnish command:
sudo varnishlog -c
and its output
* << Request >> 658516
- Begin req 658515 rxreq
- ReqMethod GET
- ReqURL /sample/2
- VCL_call HIT
- RespHeader X-Timestamp: 1460482977.61998
- RespHeader X-Varnish: 658516 658416
- RespHeader X-Varnish-Cache: HIT
From this command output I want to redirect the output to a file with following fomat:
Begin="req 658515 rxreq",ReqURL="/sample/2", RespHeader="X-Varnish-Cache: HIT"
I have used grep command to get the required fields:
sudo varnishlog -c | grep -E 'Begin|ReqURL|Varnish-Cache'
- Begin req 658515 rxreq
- ReqURL /sample/2
- RespHeader X-Varnish-Cache: HIT
But if I used additional commands to replace space and new lines I am facing issue.
sudo varnishlog -c | grep -E 'Begin|ReqURL|Varnish-Cache' | sed 's/ /=/g'
With this command I am not getting any output.
If I use sed or tr commands:
sudo varnishlog -c | sed 's/ /=/g'
or
sudo varnishlog -c | tr ' ' '='
Then output is :
*===<<=Request==>>=629459====
-===Begin==========req=629458=rxreq
-===ReqMethod======GET
-===ReqURL=========/sample/2
-===VCL_call=======HIT
-===RespHeader=====X-Varnish-Cache:=HIT
If I use this:
sudo varnishlog -c | sed 's/\t/=/g'
Then output is same as original :
* << Request >> 658516
- Begin req 658515 rxreq
- ReqMethod GET
- ReqURL /sample/2
- VCL_call HIT
- RespHeader X-Timestamp: 1460482977.61998
- RespHeader X-Varnish: 658516 658416
- RespHeader X-Varnish-Cache: HIT
please help me with some hints on what is the correct way to get my required output.
@Sundeep, Perl version on my machine is :
perl -version
This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi
(with 44 registered patches, see perl -V for more detail)
@Sundeep,
Output of sudo varnishlog -c | cat -A
$
* << Request >> 363192 $
- Begin req 363191 rxreq$
- Timestamp Start: 1478514424.525802 0.000000 0.000000$
- Timestamp Req: 1478514424.525802 0.000000 0.000000$
- ReqStart 10.56.36.2 52583$
- ReqMethod GET$
- ReqURL /sample/2$
- ReqProtocol HTTP/1.1$
- ReqHeader Host: localhost:6081$
- ReqHeader User-Agent: Mozilla/5.0 (X11; Ubuntu;....$
- ReqHeader Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8$
- ReqHeader Accept-Language: en-US,en;q=0.5$
- ReqHeader Accept-Encoding: gzip, deflate$
- ReqHeader Connection: keep-alive$
- ReqHeader Pragma: no-cache$
- ReqHeader Cache-Control: no-cache$
- ReqHeader X-Forwarded-For: 10.56.36.2$
- VCL_call RECV$
- VCL_return hash$
- ReqUnset Accept-Encoding: gzip, deflate$
- ReqHeader Accept-Encoding: gzip$
- VCL_call HASH$
- VCL_return lookup$
- Hit 658416$
- VCL_call HIT$
- VCL_return deliver$
- RespProtocol HTTP/1.1$
- RespStatus 200$
- RespReason OK$
- RespHeader Content-Length: 4774$
- RespHeader Last-Modified: Tue, 12 Apr 2016 17:42:58 GMT$
- RespHeader Etag: f9d34a65e8c1b30245c2f12534348ff9$
- RespHeader X-Timestamp: 1460482977.61998$
- RespHeader Content-Type: image/png$
- RespHeader X-Trans-Id: txf52283ea65004e578ddfe-0058203d84$
- RespHeader Date: Mon, 07 Nov 2016 08:38:28 GMT$
- RespHeader X-Varnish: 363192 658416$
- RespHeader Age: 6516$
- RespHeader Via: 1.1 varnish-v4$
- VCL_call DELIVER$
- RespHeader X-Varnish-Cache: HIT$
- VCL_return deliver$
- Timestamp Process: 1478514424.525880 0.000078 0.000078$
- RespHeader Accept-Ranges: bytes$
- Debug "RES_MODE 2"$
- RespHeader Connection: keep-alive$
- Timestamp Resp: 1478514424.525908 0.000106 0.000028$
- ReqAcct 415 0 415 394 4774 5168$
- End $
回答1:
maybe this could help you;
sudo varnishlog -c | grep --line-buffered -E 'Begin|ReqURL|Varnish-Cache' | sed -u 's/- //;s/ */="/;s/$/"/' | tr '\n' ','
来源:https://stackoverflow.com/questions/40462153/grep-multiple-strings-and-then-replace-text