How to GREP a substring in a line which a is variable assignment?
You can use the -o
("only") flag. This command:
grep -o 'CpuIowait=[^;]*'
will print out the specific substrings that match CpuIowait=[^;]*
, instead of printing out the whole lines that contain them.
If you want to use grep
, try something like this:
echo "OK: CpuUser=0.11; CpuNice=0.00; CpuSystem=0.12; CpuIowait=0.02; CpuSteal=0.00;" | grep -oE "CpuIowait=[[:digit:]]*\.[[:digit:]]*"
If you're not dead set on using grep
, you can pipe the input to sed
:
sed -ne "s/^.*\(CpuSystem=[0-9.]*\);.*$/\1/p;"
From man sed
:
-n, --quiet, --silent
: suppress automatic printing of pattern space
-e script, --expression=script
: add the script to the commands to be executed