格式化多个字符串,然后替换文本 [英] Grep multiple strings and then replace text

查看:113
本文介绍了格式化多个字符串,然后替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  sudo varnishlog -c 

pre>

及其输出

  *<<请求>> 658516 
- 开始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

从这个命令的输出我想将输出重定向到一个文件格式如下:

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

grep命令获取必需的字段:

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

- 开始请求658515 rxreq
- ReqURL / sample / 2
- RespHeader X-Varnish-Cache:HIT

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

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

使用这个命令我没有得到任何输出。 p>

如果我使用sed或tr命令:

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

  sudo varnishlog -c | tr'''='

然后输出结果为:

  * ===<< =请求==>> = 629459 ==== 
- ===开始====== ==== req = 629458 = rxreq
- === ReqMethod == GET GET
- === ReqURL ========= / sample / 2
- === VCL_call ======= HIT
- === RespHeader ===== X-Varnish-Cache:= HIT

如果我使用这个:

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

然后输出和原始一样:

  *<<请求>> 658516 
- 开始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

请帮我提供一些提示,说明获得我所需输出的正确方法。我的机器上的Perl版本是: / p>

  perl -version 

这是perl 5,版本18,subversion 2(v5.18.2)内置对于x86_64-linux-gnu-thread-multi
(有44个已注册的补丁,详情参见perl -V)

@Sundeep,



输出 sudo varnishlog -c | cat -A

  $ 
*<<请求>> 363192 $
- 开始请求363191 rxreq $
- 时间戳开始:1478514424.525802 0.000000 0.000000 $
- 时间戳记需求:1478514424.525802 0.000000 0.000000 $
- 需求开始10.56.36.2 52583 $
- ReqMethod GET $
- ReqURL / sample / 2 $
- ReqProtocol HTTP / 1.1 $
- ReqHeader主机:localhost:6081 $
- ReqHeader User-Agent:Mozilla / 5.0(X11; Ubuntu; .... $
- ReqHeader接受:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8 $
- ReqHeader Accept-Language:en-US,en; q = 0.5 $
- ReqHeader接受编码:gzip,deflate $
- ReqHeader连接: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查找$
- 点击658416 $
- VCL_call HIT $
- VCL_return提供$
- RespProtocol HTTP / 1.1 $
- RespStatus 200 $
- RespReason OK $
- RespHeader Content-Length:4774 $
- RespHeader Last-Modified:Tue,2016年4月12日17:42:58 GMT $
- RespHeader Etag:f9d34a65e8c1b30245c2f12534348ff9 $
- RespHeader X-Timestamp:1460482977.61998 $
- RespHeader Content-Type:image / png $
- RespHeader X-Trans-Id: txf52283ea65004e578ddfe-0058203d84 $
- RespHeader日期:2016年11月07日星期一08:38:28 GMT $
- RespHeader X-Varnish:363192 658416 $
- RespHeader年龄:6516 $
- RespHeader通过:1.1 varnish-v4 $
- VCL_call DELIVER $
- RespHeader X-Varnish-Cache:HIT $
- VCL_return交付$
- 时间戳处理:1478514424.525880 0.000078 0.000078 $
- RespHeade r接受范围:字节$
- 调试RES_MODE 2$
- RespHeader连接:保持活动$
- 时间戳响应:1478514424.525908 0.000106 0.000028 $
- ReqAcct 415 0 415 394 4774 5168 $
- End


解决方案

也许这可以帮助你;

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


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            $

解决方案

maybe this could help you;

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

这篇关于格式化多个字符串,然后替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆