Question

I have a Linux command line to display a certain result. From the result, I would like to grep it or use awk to pull out a specific value. This is the result of the command line:

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

All I want is to display on the value after 'active'=? which would be in this instance 0

Was it helpful?

Solution

try this grep line:

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

OTHER TIPS

You could use egrep like this :

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

Example :

[ ~]$ 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
[ ~]$ 

If you're sure that the right value is numeric, you can further restrict the pattern like this :

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

You could use sed too :

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

And if you want to get only the right value :

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

You could use awk too but I think that is not the better solution in this case. But just for fun :

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

N.B : egrep is equivalent to "-E" option of grep.

Another gnu awk

awk '{print gensub(/.*active.=(.*) .*/,"\\1","g")}' file
0
awk '{ match($0,/'active'=(.*) /,a); print a[1]}' file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top