gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:45:32 25 4
gpt4 key购买 nike

我有以下 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

从此命令输出中,我想使用以下格式将输出重定向到文件:
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 $

最佳答案

也许这可以帮助您;

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

关于unix - Grep 多个字符串,然后替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com