1

So I have this json file that's an output from a previous command:

{
    "upload": {
        "status": "INITIALIZED", 
        "contentType": "application/octet-stream", 
        "name": "mobile.apk", 
        "created": 1511111178.799, 
        "url": "https://amazonaws.com/arn%358%3Aproject%3A0030e-0a929/uploads/arn%3Aaws%3??/33bgb", 
        "type": "ANDROID_APP", 
        "arn": "arn:aws:devicefarm:us-west-2:588:upload:0000-aaa-bg01-11ab11/1feebbb-1a1a"
    }
}

I want to extract the "arn" value, including the quotes, so the value:

"arn:aws:devicefarm:us-west-2:588:upload:0000-aaa-bg01-11ab11/1feebbb-1a1a"

Should be returned. I'll also need a separate command to extract the url value also, so the following needs to be returned:

"https://amazonaws.com/arn%358%3Aproject%3A0030e-0a929/uploads/arn%3Aaws%3??/33bgb"

So I need 2 separate commands for these values. I've tried all different combinations of grep and I've not got anything to work yet.

I only want to use grep for the command, my situation won't allow to install any tools.

Thank you!

DonJuanEco
  • 133
  • 2
  • 10

1 Answers1

1

There is couple of possible solutions, depending on a tool set that is available for you. Lets assume that bash variable JSON contains whole JSON structure you described in the topic.

Bash + Python:

ARN=`echo "$JSON" | python -c "import sys, json; print json.load(sys.stdin)['upload']['arn']"`
URL=`echo "$JSON" | python -c "import sys, json; print json.load(sys.stdin)['upload']['url']"`

Bash + jq:

URL=`echo "$JSON" | jq -r '.upload.url'`
ARN=`echo "$JSON" | jq -r '.upload.arn'`

Bash + grep + awk

URL=`echo "$JSON" | grep -Po '"url":.*?".*?"' | awk '{ print $2 }'`
ARN=`echo "$JSON" | grep -Po '"arn":.*?".*?"' | awk '{ print $2 }'`

Now you can do

echo "$ARN"
echo "$URL"

But as it was already mentioned by others, don't use awk, grep, sed and any other tools for text processing. Use dedicated tools for this.

Kamil
  • 2,712
  • 32
  • 39