2

The aws command returns a json string to stdout inside a bash shell.

$ aws ssm get-parameter --name /mysite/dev/email
{
    "Parameter": {
        "Name": "/mysite/dev/email",
        "Type": "String",
        "Value": "admin@example.com",
        "Version": 1
    }
}

I want to return the value of "Value" without the quotes wrapping it. I used a combination of awk and sed linux programs. Basically, awk searches each line in the input and allows you to return a line that has a matching value. Since awk scans a file line by line, allowing you to split the line into fields and perform action on matched pattern, I did this first:

$ aws ssm get-parameter --name /mysite/dev/email | awk -F: '/Value/ {print $2}'

"admin@example.com",

As you can see, I used : as a delimited via the F flag. I matched the line with the string "Value" and then printed the second part of the delimited string, which is the value "user@example.com",

But the quotes and comma are unwelcomed. So I had to use sed with the -E regex flag to replace the quotes and comma:

$ aws ssm get-parameter --name /mysite/dev/email | awk -F: '/Value/ {print $2}' | sed -E 's/"|",//g'
 admin@example.com

I got the desired result, but I would prefer to just use awk, instead of having to pipe awk to sed. Is it possible?

  • 1
    The jq program is designed to manipulate json data. – icarus Jan 22 at 2:45
  • ok, that's good to know. I just have to run this in an aws lifecycle hook, which executes a shell script, and to avoid any possible issues during the deploy, I planned to stay away from installing packages e.g. apt-get install jq – Donato Jan 22 at 3:07
3

For a one-liner, you can just remove all commas and quotes first.

awk '/Value/ { gsub(/[",]/,""); print $2}'

A better translation of your awk | sed pipeline would be

awk '/Value/ { gsub(/[",]/,"",$2); print $2}'

to just alter the values in the second field.

| improve this answer | |
  • Note that if the value of e.g. Name contains the string Value, this may return unexpected data. – Kusalananda Jan 22 at 16:59
  • @Kusalananda Indeed. Using something like jq to parse the data rather than pattern match is clearly the better solution (like using sh rather than csh). The answer given is to the question as asked. I pointed out in the comments that jq would be a suitable tool, and the OP clarified that he didn't want to install any extra packages. – icarus Jan 22 at 17:04
0

Using jq to parse out the value of the Value key in the top-level Parameter structure:

aws ssm get-parameter --name /mysite/dev/email | jq -r '.Parameter.Value'

This will additionally decode any JSON encoding of the value, so that a value that is stored in JSON as \"The Admin\" <admin@example.com> comes back as "The Admin" <admin@example.com>.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.