Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用shell脚本获取日志模式中的特定键值_Linux_Shell - Fatal编程技术网

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
命令