Use awk
like this:
URL=$(awk -F\" '/^webURL/{print $2}' sample.conf)
echo $URL
http://www.sampledomain:8080
The $()
means "put the result of running the enclosed command into the variable URL
". The awk
then sets the separator for fields to the double quote sign. It then looks in your file for a line that starts with "webURL" and when it finds it, it outputs everything between the second pair of double quotes, i.e. field 2 on the line.
EDITED to answer further question
If you want to parse out a subscribe_key
value from a java file called thing.java
, that looks like this:
subscribe_key = 'sub-f-xx-xx-xx-xx-xx';
you can use this:
key=$(awk -F\' '/^subscribe_key/{print $2}' thing.java)
echo $key
sub-f-xx-xx-xx-xx-xx
Note that I changed the double quote after -F
to single quote to tell awk
that fields are now separated by single quotes.
Note that if you have values that are marked by single quotes and double quotes IN THE SAME FILE you can tell awk
to use either single quotes or double quotes as field separators like this:
value=$(awk -F"\'|\"" '/^subscribe_key/{print $2}' yourFile)
echo $value
sub-f-xx-xx-xx-xx-xx
value=$(awk -F"\'|\"" '/^webURL/{print $2}' yourFile)
echo $value
http://www.sampledomain:8080