Linux 使用shell脚本获取日志模式中的特定键值
我的日志文件具有以下模式:-Linux 使用shell脚本获取日志模式中的特定键值,linux,shell,Linux,Shell,我的日志文件具有以下模式:- tx=267c5660-c49a-4ae7-b5ae-c9d43e23b617, rh=163.172.0.0, userId=-1, requestComplete={ requestId=74421156932, entityResourceType=xyz, pageId=homePage, uri=/home/, duration(ms)=422 我怎样才能只为一个特定字段(如uri或duration或uri和duration两者的组合)进行grep 基
tx=267c5660-c49a-4ae7-b5ae-c9d43e23b617, rh=163.172.0.0, userId=-1, requestComplete={ requestId=74421156932, entityResourceType=xyz, pageId=homePage, uri=/home/, duration(ms)=422
我怎样才能只为一个特定字段(如uri
或duration
或uri
和duration
两者的组合)进行grep
基本上,当我使用tail命令时,我只希望在o/p中有两个字段uri和duration 对于类似
var=$'2017-04-21 09:04:42,649 +0000 [exec-12056] EventLogger - cid=rio, tx=267c5660-c49a-4ae7-b5ae-c9d43e23b617, rh=163.172.0.0, userId=-1, requestComplete={ requestId=74421156932, entityResourceType=xyz, pageId=homePage, uri=/riokc95758/, duration(ms)=422'
<>你可以考虑GNU-GRP: < /P>
$ grep -Po 'uri=\K.[^,]*' <<<"$var"
/riokc95758/
$ grep -Po 'uri=/\K.[^,/]*' <<<"$var"
riokc95758
$ grep -Po '.*duration\(ms\)=\K.[^,]*' <<<"$var" #if duration is the last field you can use just grep -Po '.*duration\(ms\)=\K.*'
422
$grep-Po'uri=\K.[^,]*'对于类似
var=$'2017-04-21 09:04:42,649 +0000 [exec-12056] EventLogger - cid=rio, tx=267c5660-c49a-4ae7-b5ae-c9d43e23b617, rh=163.172.0.0, userId=-1, requestComplete={ requestId=74421156932, entityResourceType=xyz, pageId=homePage, uri=/riokc95758/, duration(ms)=422'
<>你可以考虑GNU-GRP: < /P>
$ grep -Po 'uri=\K.[^,]*' <<<"$var"
/riokc95758/
$ grep -Po 'uri=/\K.[^,/]*' <<<"$var"
riokc95758
$ grep -Po '.*duration\(ms\)=\K.[^,]*' <<<"$var" #if duration is the last field you can use just grep -Po '.*duration\(ms\)=\K.*'
422
$grep-Po'uri=\K.[^,]*'我们可以使用cut命令。根据日志,有一个分隔符,即“,”。所以我们可以使用“,”作为delimeter,我们需要给出字段号,在我们的例子中,uri和持续时间是8,9
cat "logfile" | cut -d"," -f7,8
谢谢我们可以使用cut命令。根据日志,有一个分隔符,即“,”。所以我们可以使用“,”作为delimeter,我们需要给出字段号,在我们的例子中,uri和持续时间是8,9
cat "logfile" | cut -d"," -f7,8
谢谢如果模式具有相同数量的逗号分隔值,则可以使用cut
。顺序也必须是一致的
否则,双grep可以获得您想要的值:
grep -Eo "uri=/.*/" | grep -Eo "/.*/" # gets uri
grep -Eo "duration\(ms\)=[0-9]+" | grep -Eo "[0-9]+" # gets duration
说明:
在第一个示例中,输入的grep-Eo“uri=/.*/”
将返回duration(ms)=422
。用第二个grep链接它可以让您单独隔离422
这有点类似于如果模式具有相同数量的逗号分隔值,则可以使用cut
。顺序也必须是一致的
否则,双grep可以获得您想要的值:
grep -Eo "uri=/.*/" | grep -Eo "/.*/" # gets uri
grep -Eo "duration\(ms\)=[0-9]+" | grep -Eo "[0-9]+" # gets duration
说明:
在第一个示例中,输入的grep-Eo“uri=/.*/”
将返回duration(ms)=422
。用第二个grep链接它可以让您单独隔离422
这有点类似于UOOC。跳过cat
和管道,只需将文件名附加到cut
commanduooc。跳过cat
和管道,只需将文件名附加到cut
命令