find a pattern from grep in bash for computing ave

2020-05-07 06:21发布

问题:

I want to get the average memory usage of some android app. All I want to do is to fetch only the memory usage. For example, below script provide instantaneous memory usage of mobile_cep application

adb shell dumpsys meminfo | grep mobile_cep

gives output as

233,328K: org.carleton.iot.mobile_cep (pid 27060 / activities)

While I am interested in getting 233,328K value. I want to repeat this process several times and get an average value for memory usage. I am using below script to print 233,328K value.

#!/bin/bash
counter=1
while [ $counter -le 10 ]
    do
        ((counter++))
        val1=$(adb shell dumpsys meminfo | grep mobile_cep)
        val2=$($val1 | grep -i '\d\d\d,\d\d\dK') 
        echo $val2
    done
echo  done

however, I am not getting the desired result. What am I doing wrong?

回答1:

Piping to sed:

adb shell dumpsys meminfo | grep mobile_cep | sed 's/:.*//'

It is also trivial with awk if you are able to execute an awk script you don't need grep either:

adb shell dumpsys meminfo | awk -F'[: ]' '/mobile_cep/ { print $1 }'