Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get characters after specific substring from string in Bash

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading