0

How extract specific data from my variable in Bash ?

myvar={"tag":"somedata", "token":"dataIWant", "...":"..."};

I 'm not very comfortable with unix command, specially regex.

Laetitia28
  • 95
  • 10
  • 1
    Your data looks like JSON, do you confirm your data is the string `{"tag":"somedata", "token":"dataIWant", "...":"..."}`? Could you also specify what you want to extract so it is easier to answer you? – Aaron Nov 05 '15 at 14:12
  • 3
    Bash itself possess no feature for parsing JSON directly. However, I would heartily recommend [jq](https://stedolan.github.io/jq/) as a useful, command-line tool for interacting with JSON. – kojiro Nov 05 '15 at 14:20
  • In fact , I get my variable from a curl request. I want to retrive "dataIWant". – Laetitia28 Nov 05 '15 at 14:48
  • I think we had another question just like this yesterday -- including the mistaken wanting-to-use-a-regex aspect (despite regular expressions not being tools appropriately suited to parsing irregular languages). [See http://cstheory.stackexchange.com/questions/3987/is-json-a-regular-language ]. – Charles Duffy Nov 05 '15 at 16:23

1 Answers1

1

Taking kojiro's suggestion:

myvar=$( curl ... )
value=$( jq -r '.token' <<< "$myvar" )
echo "$value"   # ==> dataIWant
glenn jackman
  • 238,783
  • 38
  • 220
  • 352