I want to store a specific piece of a string as a variable in Bash.
I have this string:
data="
prop1: v1
prop2: v2
prop3: v3
"
I use this code to select the v2 from the string:
subStr=$(echo "$data" | awk '"prop2:" {print $2}')
But the value of subStr is empty…
I tried using grep for searching prop2 and select the following characters but i did not get that working.
>Solution :
Match the first field against prop2: at the beginning of string:
$ subStr=$(awk '$1 ~ /^prop2:/ {print $2}' <<<"$data")
$ echo "$subStr"
v2