1

I am getting a list of quoted strings back from AWS, and I would like to isolate one of them into a parameter.

the command I am using is:

allelb=$(aws elb describe-load-balancers --query 'LoadBalancerDescriptions[].LoadBalancerName') &&  echo $allelb

And this is the output I am getting:

[ "elb-app-mprest-dev", "elb-core-mprest-dev", "api-vector-k8s-local-0j8ccl", "a2e6a899d111011e897b0067693cf815", "api-clusters-sydney7-mpre-rqae1h" ]

What I want to do is, get only the string with the word 'sydney7' inside a parameter. So I will have a new parameter with this content inside :

ELB=api-clusters-sydney7-mpre-rqae1h

This is waht I got so far:

allelb=$(aws elb describe-load-balancers --query 'LoadBalancerDescriptions[].LoadBalancerName' | tr -d '"' | tr -d ',' | tr -d ']' | tr -d '[') &&  echo $allelb | grep -o sydney7

but the output I get is not good enough-

sydney7

How can I achieve that?

4
  • 1
    Lots of ways... Commented Feb 28, 2018 at 15:04
  • 1
    I will edit the question to show the ways I tried Commented Feb 28, 2018 at 15:05
  • 1
    What if there's more than one such a string? Commented Feb 28, 2018 at 15:06
  • @choroba There will be no more than one from this string since this name is unique Commented Feb 28, 2018 at 15:08

2 Answers 2

3

Since the output from your command is a JSON array, you should probably use a program for parsing and filtering JSON, like jq:

$ jq '.[] | match(".*sydney.*").string' <<< "$allelb"
"api-clusters-sydney7-mpre-rqae1h"

If you want it raw, use the -r flag:

$ jq -r '.[] | match(".*sydney.*").string' <<< "$allelb"
api-clusters-sydney7-mpre-rqae1h
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but the assumption is that I can not install jq on my machine, I gave it a vote up :)
1

With cut

cut -d '"' -f10

with grep

grep -o '[^"]*sydney7[^"]*'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.