繁体   English   中英

从结果Linux中提取价值

[英]Extract value from result Linux

我有一个Linux命令行来显示特定结果。 从结果来看,我想grep它或使用awk提取特定值。 这是命令行的结果:

OK 0 seconds over max, 0 active processes, next process in the future|'overmax'=0s;300;600 'active'=0 'nextoldest'=-1153s

我只想在'active'=之后的值上显示? 在这种情况下为0

试试这个grep行:

grep -Po "'active'=\K\S*"

您可以这样使用egrep:

egrep -o "'active'=[^\ ]+"

范例:

[ ~]$ str="OK 0 seconds over max, 0 active processes, next process in the future|'overmax'=0s;300;600 'active'=0 'nextoldest'=-1153s"
[ ~]$ echo $str|egrep -o "'active'=[^\ ]+"
'active'=0
[ ~]$ 

如果您确定正确的值是数字,则可以进一步限制模式,如下所示:

egrep -o "'active'=[0-9]+"

您也可以使用sed:

[ ~]$ echo $str|sed "s/.*\('active'=[^\ ]*\).*/\1/g"
'active'=0

如果您只想获得正确的值:

[ ~]$ echo $str|sed "s/.*'active'=\([^\ ]*\).*/\1/g"
0

您也可以使用awk,但我认为在这种情况下这不是更好的解决方案。 但是只是为了好玩:

[ ~]$ echo $str|awk -F " " '{for (i=1; i <= NF; i++){if($i ~ ".?active.?="){print $i}}}'
'active'=0

注意:egrep等效于grep的“ -E”选项。

另一个gnu awk

awk '{print gensub(/.*active.=(.*) .*/,"\\1","g")}' file
0
awk '{ match($0,/'active'=(.*) /,a); print a[1]}' file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM