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.


NETWORK=`cat generalInfo.info | grep network | awk '{print $2}'`

if [ $NETWORK -eq "Purple" ]; then
    echo "This network is Purple"
    echo "This network is not Purple"

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.

  • 2
    If it really is json, then you're better off parsing it with a tool that understands json. If a tool outputted the generalInfo.info file, can it be persuaded to output in a different format, or only the value you want?
    – Jeff Schaller
    Jan 20, 2021 at 17:49
  • I don't have any control over the tool that outputted the file. But I do know it'll be structured the same way every time. It just contains information from which I can base some of my script logic. I think I've found a way with the following: cat generalInfo.info | grep network | awk -F '"' '{print $4}' Jan 20, 2021 at 17:59
  • 1
    @dutsnekcirf you never need cat or grep if you're using awk, for example cat 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.
    – Ed Morton
    Jan 21, 2021 at 14:14

3 Answers 3


There are many ways of doing it. However, as already mentioned in the comments, if this is actually structured JSON data, you might want to look into things like jq that are designed to parse JSON.

For the simple case you mention, here are a few options:

  1. awk

     $ echo '"network": "Purple",' | awk '/network/{ gsub(/[",]/,"",$2); print $2}'
  2. GNU grep

     $ echo '"network": "Purple",' | grep -oP '"network":\s*"\K[^"]+'
  3. sed

     $ echo '"network": "Purple",' | sed -n 's/"network": *"\([^"]*\).*/\1/p'


     $ echo '"network": "Purple",' | sed -En 's/"network":\s*"([^"]+).*/\1/p'

Finally, try to avoid using ALLCAPS variable names in shell scripts. By convention, global environment variables are in ALLCAPS and this can lead to confusion if you also have your own variable names capitalized. In the specific case of your script, you don't even need a variable. I would rewrite your script like this:


grep -q 'network.*Purple' generalInfo.info &&
    echo "This network is Purple" ||
    echo "This network is not Purple"

You don't need to cat a file in order to grep it, and you don't need to store something in a variable to check if it exists.


With sed you can try this:

sed 's/^.*: "\(.*\)",$/\1/' generalInfo.info

This variant of cut:

cut -d'"' -f4 generalInfo.info

With grep:

grep -Po '(?<=": ").*(?=",)' generalInfo.info
$ var=$(awk -F'"' '$2=="network"{print $4}' file)
$ echo "$var"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .