0

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.

3
  • 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
    Commented 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}' Commented 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
    Commented Jan 21, 2021 at 14:14

3 Answers 3

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}'
     Purple
    
  2. GNU grep

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

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

    or

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

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:

#!/bin/bash

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.

0
2

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
0
1
$ var=$(awk -F'"' '$2=="network"{print $4}' file)
$ echo "$var"
Purple

You must log in to answer this question.

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