In bash, I'm reading a string from a file that looks like the following:
"network": "Purple",
How would I use grep, awk or even sed to pull out just the string: "Purple" without the quotes and assign it to a variable?
I need to strip out the quotation marks and the comma at the end.
There are instances in which it may not contain the word Purple but could be some other color but will always be a single word.
I've been trying the following code but can't get it perfect.
#!/bin/bash
NETWORK=`cat generalInfo.info | grep network | awk '{print $2}'`
if [ $NETWORK -eq "Purple" ]; then
echo "This network is Purple"
else
echo "This network is not Purple"
fi
exit 0
This code so far gets me "Purple", but need to strip out the surrounding quotes and comma. Would it be easier to use the double quote as the delimiter for awk?
The generalInfo.info file is just a text file with json like formatting. I've read that I could use jq to parse the file but in my particular instance I don't have jq installed and can not install it.
Thanks for any help.
cat
orgrep
if you're usingawk
, for examplecat generalInfo.info | grep network | awk -F '"' '{print $4}'
=awk -F '"' '/network/{print $4}' generalInfo.info
but use of/network/
unbounded across the whole output line is fragile, I wouldn't do it as it'll cause false matches.