我有一個Linux命令行來顯示某個結果。從結果中,我想grep它或使用awk來提取特定的值。這是命令行的結果:從結果中提取值Linux
OK 0 seconds over max, 0 active processes, next process in the future|'overmax'=0s;300;600 'active'=0 'nextoldest'=-1153s
我只想在'active'=?之後顯示值。這將是在這種情況下0
我有一個Linux命令行來顯示某個結果。從結果中,我想grep它或使用awk來提取特定的值。這是命令行的結果:從結果中提取值Linux
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*"
awk '{ match($0,/'active'=(.*) /,a); print a[1]}' file
另一個gnu awk
awk '{print gensub(/.*active.=(.*) .*/,"\\1","g")}' file
0
你可以使用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
N.B:egrep相當於grep的「-E」選項。
輝煌的,這沒有把戲。只是所以我明白這一點,你能解釋一下在'active'=''K'之後regEx位 – Chelseawillrecover
@Chelseawillrecover'-P PCRE','-o only','\ S *'所有非空字符積極的背後,但動態的長度。 – Kent