Skip to content

Instantly share code, notes, and snippets.

@shadowbrain
Created December 16, 2011 15:32
Show Gist options
  • Save shadowbrain/1486478 to your computer and use it in GitHub Desktop.
Save shadowbrain/1486478 to your computer and use it in GitHub Desktop.
parse json key:value with sed
## Example: Grab the value for the MYSQL_USER key
User=$( sed -n 's/.*"MYSQL_USER": "\(.*\)",/\1/p' /var/lib/credentials.json )
## Example: Grab the value for the MYSQL_PASSWORD key
Passwd=$( sed -n 's/.*"MYSQL_PASSWORD": "\(.*\)",/\1/p' /var/lib/credentials.json )
@akapoor47
Copy link

JSON is stored in variable called - token_response

{ "Code":"Success", "LastUpdated":"2018-12-27T07:16:31Z", "Type":"fakedTypeValue", "AccessKeyId":"fakedAccessKeyIdValue", "Token":"fakedTokenValue" }

When trying to fetch using - echo aws_access_key_id is:$(echo $token_response | sed -n 's/^.*"AccessKeyId":"\([^"]*\)",*$/\1/p')

Following is printed on console without the token value (Seems value for AccessKeyId is not parsed) - aws_access_key_id is:

I wonder what's wrong in my regex above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment