Grep多个字符串,然后替换文本

时间:2016-11-07 09:49:58

标签: unix sed grep varnish

我有以下清漆命令:

sudo varnishlog -c

及其输出

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

从这个命令输出我想将输出重定向到一个带有以下fomat的文件:

Begin="req 658515 rxreq",ReqURL="/sample/2", RespHeader="X-Varnish-Cache: HIT"

我使用grep命令获取必填字段:

sudo varnishlog -c | grep -E 'Begin|ReqURL|Varnish-Cache'

-   Begin          req 658515 rxreq 
-   ReqURL         /sample/2
-   RespHeader     X-Varnish-Cache: HIT

但是,如果我使用其他命令来替换空间和新线路,我将面临问题。

sudo varnishlog -c | grep -E 'Begin|ReqURL|Varnish-Cache' | sed 's/ /=/g'

使用此命令我没有得到任何输出。

如果我使用sed或tr命令:

sudo varnishlog -c | sed 's/ /=/g'

sudo varnishlog -c | tr ' ' '='

然后输出是:

*===<<=Request==>>=629459====
-===Begin==========req=629458=rxreq
-===ReqMethod======GET
-===ReqURL=========/sample/2
-===VCL_call=======HIT
-===RespHeader=====X-Varnish-Cache:=HIT

如果我使用它:

sudo varnishlog -c | sed 's/\t/=/g'

然后输出与原始相同:

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

请帮助我提供一些关于获得所需输出的正确方法的提示。

@Sundeep,我机器上的Perl版本是:

 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,

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 个答案:

答案 0 :(得分:0)

也许这可以帮到你;

sudo varnishlog -c | grep --line-buffered -E 'Begin|ReqURL|Varnish-Cache' |  sed -u 's/-   //;s/   */="/;s/$/"/' | tr '\n' ','