How to GREP a substring in a string that is a destination variable?
4 answers
To get the whole line:
var=$(grep -Eo 'CpuIowait=[0-9]{1,2}[.][0-9]{2}' < <(command))
To only get the value if you have GNU grep with -P you can use the following:
var=$(grep -Po '(?<=CpuIowait=).*(?=;)' < <(command))
Using awk:
var=$(awk -F'; |: ' '{ for (i=0;i<=NF;i++) { split($i,arr,/=/); if (arr[1] == "CpuIowait") { print arr[2] } } }' < <(command))
Using pure bash:
output=$(command)
[[ $output =~ (CpuIowait=[0-9][.][0-9]{2}) ]] && echo "${BASH_REMATCH[1]}"
+1
source to share