0

I have use a command which outputs the following. .

"Credentials": {
        "AccessKeyId": "ASIARKHY6",
        "SecretAccessKey": "FHM11kEwWZ",
        "SessionToken": "IQoJb3JpZ2luX2",
}

I then have to export each of the 3 following keys. I am doing this manually but would like to write script.

I can pipe the result of the command into a tmp file and then use

cat tmpfile.tmp |grep  AccessKeyId 

Which outputs : "SecretAccessKey": "FHM11kEwW6sP3Z"

How can I then I select just the text with the second set of "". Which I can then export.

1
  • 3
    If the output of the command is a proper JSON object, you could do this easily with a JSON parser such as jq. But the snippet you have shown here isn't valid JSON.
    – Haxiel
    Jul 9, 2020 at 8:17

4 Answers 4

0

Assuming you have the output of your program stored into the file tmpfile.tmp, you can extract a specific value to a specific key by using just the sed command, as follows, i.e., using the "AccessKeyId" key:

key=AccessKeyId;
sed -n 's/^.*\"'$key'\":.*\"\(.*\)\".*$/\1/p' tmpfile.tmp

which outputs ASIARKHY6. You could also just pipe directly to output of your program to sed, without creating a temporary file as follows:

key=AccessKeyId;
./program | sed -n 's/^.*\"'$key'\":.*\"\(.*\)\".*$/\1/p'

I suggest you to put one of the two forms written above into a script (let's call it getval.sh) as follows:

#!/bin/bash
if [ $# -lt 3 ];then echo "usage: $0 <inputfile> <key>"; exit 0 ;fi
infile=$1
key=$2
sed -n 's/^.*\"'$key'\":.*\"\(.*\)\".*$/\1/p' $infile

which, after making it executable, you could run as follows:

./getval.sh tmpfile.tmp AccessKeyId
0
0

Assuming the command you run should output a credential JSON output. You can use jq tool.

For example

creds_json=$(substitute the command which you used to output the credentials)
export AWS_ACCESS_KEY_ID=$(echo "$creds_json" | jq -r .credentials.accessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo "$creds_json" | jq -r .credentials.secretAccessKey)
export AWS_SESSION_TOKEN=$(echo "$creds_json" | jq -r .credentials.sessionToken)
0

Assuming that your input is a well-formed JSON document, with a Credentials key as one of the top-level keys:

{
  "Otherinfo": "blah blah",
  "Credentials": {
    "AccessKeyId": "ASIARKHY6",
    "SecretAccessKey": "FHM11kEwWZ",
    "SessionToken": "IQoJb3JpZ2luX2",
    "SecretStuff": "njahnjah"
  }
}

You may then extract any of the sub-keys' values by using something like

eval "$(
    jq -r '
        $ARGS.positional[] as $key | @sh "export \($key)=\(.Credentials[$key])"' file \
        --args AccessKeyId SecretAccessKey SessionToken
)"

With the input above, this would generate and evaluate the following shell commands:

export 'AccessKeyId'='ASIARKHY6'
export 'SecretAccessKey'='FHM11kEwWZ'
export 'SessionToken'='IQoJb3JpZ2luX2'

You can get upper-case variable names by using $key|ascii_upcase in place of just $key in the jq expression.

If you're reading from a command rather than from a file, then use a pipe:

eval "$(
    some-command |
    jq -r '
        $ARGS.positional[] as $key | @sh "export \($key|ascii_upcase)=\(.Credentials[$key])"' \
        --args AccessKeyId SecretAccessKey SessionToken
)"
0

I am assuming you want the output to be enclosed in double quotes.

Using sed

$ export AccessKeyId=$(sed -n s'/.*AccessKeyId[^"]*\("[^"]*"\).*/\1/p' input_file)

Using grep

$ export AccessKeyId=$(grep -Po '.*AccessKeyId[^"]*\K"[^"]*"' input_file)

Using awk

$ export AccessKeyId=$(awk -F": |," '/AccessKeyId/{print $2}' input_file)

Output

"ASIARKHY6"

You must log in to answer this question.

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