[英]Using sed to separate pattern from stream
I have a file that has log entries on each line like: 我有一个在每行上都有日志条目的文件,例如:
request country_US url=http://example.com/us/id=6546456 {response_time 1000 msec}
request country_UK url=http://example.com/uk/id=1242423 {response_time 60 msec}
Right now I'm using sed to process this file and separate the response time like so: 现在,我正在使用sed处理该文件并分隔响应时间,如下所示:
sed -e 's/.*\(response_time \S\+\).*/\1/p' -e 's/'
That transforms each line to 这会将每一行转换为
response_time 1000
response_time 60
Right now I would like to separate also the country prefix and get processed lines like so: 现在,我想也将国家/地区前缀分开,并像这样获得处理后的行:
US 1000
UK 60
How should i modify my sed command? 我应该如何修改sed命令? Thanks!
谢谢!
尝试这个:
sed -e 's/^request country_\([^ ]*\).*response_time \([0-9]*\) msec.*$/\1 \2/g'
You could modify your sed
expression a bit to get the desired result. 您可以稍微修改
sed
表达式以获得所需的结果。 Saying: 说:
sed -r 's/.*country_(\S+).*response_time ([0-9]+).*/\1 \2/' filename
would return the following for your sample input: 将为您的示例输入返回以下内容:
US 1000
UK 60
Using awk
使用
awk
awk '/response_time/ {split($2,a,"_");print a[2],$(NF-1)}' file
US 1000
UK 60
If all line does have response_time
you can just do: 如果所有行都具有
response_time
,则可以执行以下操作:
awk '{split($2,a,"_");print a[2],$(NF-1)}' file
Or just: 要不就:
awk -F" |_" '{print $3,$7}' file
US 1000
UK 60
Another possibility which is pretty much in the sence of your original version is: 原始版本中存在的另一种可能性是:
sed -e 's/.*country_\(\S\+\).*response_time\( \S\+\).*/\1\2/' file
US 1000
UK 60
For the future I can only recommend http://vimregex.com/ . 将来我只能推荐http://vimregex.com/ 。 I always found it being extremely useful.
我总是发现它非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.