1

Hi how do I get only the value test-staging-100 in the following dictionary

{ 
   "clusters": [
       "test-staging-100", 
       "test-local-1", 
       "test-dev-50" 
     ] 
}

when I ran this command

$cluster = aws list-clusters | Select-String "test-staging"

I keep getting "test-staging-100", . I don't want the quotation and comma but only test-staging-100

1 Answer 1

1

It looks like what the aws CLI returns is JSON text, so it's preferable to parse it into an object graph with ConvertFrom-Json and act on that:

[string] $cluster = 
  @((aws list-clusters | ConvertFrom-Json).clusters) -match '^test-staging'

Note:

  • The @(...) ensures that -match also works as intended - as a filter returning the matching element(s) - in case only one cluster happens to be returned.

  • When -match acts as a filter, it invariably returns an array, even if only one element of the LHS array matches; type constraint [string] converts such a one-element array to a single string.

  • If there's a chance that multiple elements match, more work is needed; to categorically use only the first, enclose the RHS of the assignment in (...)[0]


If you really wanted to use Select-String to search the raw string output (which is ill-advised), you'd have to do something like:

$cluster = (aws list-clusters | Select-String 'test-staging[^"]+').Matches.Value
Sign up to request clarification or add additional context in comments.

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.