[英]Replacing String in log Using Sed
I have access log like: 我有如下访问日志:
10.2.21.120 - - [26/Jan/2013:19:15:11 +0000] "GET /index.html HTTP/1.1" 200 6 "-" "Mozilla/0.0 (X11; Linux x86_64) AppleWebKit/000.00 (KHTML, like Gecko) Chrome/0.0.0000.00 Safari/000.00"
I want to replace the user-agent. 我要替换用户代理。 Resulting:
结果:
10.2.21.120 - - [26/Jan/2013:19:15:11 +0000] "GET /index.html HTTP/1.1" 200 6 "-" "NetScape"
I try to change all after "-"
but Sed skips ""
and change all after -
我尝试将所有
"-"
之后的内容更改,但Sed跳过""
并更改所有内容-
something like this sed 's/[(][^)]*[)]/\\(NetScape\\)/g'
input` change user-agent only in brackets 像
sed 's/[(][^)]*[)]/\\(NetScape\\)/g'
g'input`只能在方括号中更改用户代理
Using sed
, you may replace the substring between quotes at the end of the line: 使用
sed
,您可以在行尾替换引号之间的子字符串:
sed 's/"[^"]*"$/"NetScape"/'
Here, "[^"]*"$
matches "
, then 0+ chars other than "
and then a "
at the end of the line. 在这里,
"[^"]*"$
匹配"
,然后0+比其他字符"
然后"
在生产线的末端。 You do not need the g
operator since sed
processes files line by line. 您不需要
g
运算符,因为sed
逐行处理文件。
See the online sed
demo : 参见在线
sed
演示 :
log='10.2.21.120 - - [26/Jan/2013:19:15:11 +0000] "GET /index.html HTTP/1.1" 200 6 "-" "Mozilla/0.0 (X11; Linux x86_64) AppleWebKit/000.00 (KHTML, like Gecko) Chrome/0.0.0000.00 Safari/000.00"'
sed 's/"[^"]*"$/"NetScape"/' <<< "$log"
# => 10.2.21.120 - - [26/Jan/2013:19:15:11 +0000] "GET /index.html HTTP/1.1" 200 6 "-" "NetScape"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.