-1

I am working on a shell script to capture all instances names (only) in an az. I am using aws-cli to fetch the list. When i fire the command via script i get the list which is long, i have shortened for it as this is just example. I receive below response to my query.

[ [ "host_abc" ], [ "host_xyz" ], [ "host_foo" ], [ "host_bar" ] ]

I just want capture the host names only and get rid of all other special characters like those square brackets, commas and double quotes. I want to get output as copied below.

host_abc
host_xyz
host_foo
host_bar

Is there a way to filter such response stored in variable or a file in a shell script? Thank you

2
  • Q: Is there a way to filter such response stored in variable or a file in a shell script? A: If you're in a Linux shell, then your best bet is to learn a little sed: digitalocean.com/community/tutorials/…. Otherwise, for other shells (including Windows PowerShell), learn some basic regex: javatpoint.com/regex Commented Sep 5, 2022 at 21:13
  • if your data is in json format, it is best to use a json library, rather then trying to treat it as ascii strings and slice it up with sed/aqk. it will serve you well. unfortunetly, unix shell does not have built in json scripting, but there is a tool call jq. also, it is easy to create a small python script to properly read that data. Commented Sep 5, 2022 at 21:23

1 Answer 1

2

If your system has jq, it is really helpful for things like this:

aws-cli <whatever> | jq -r 'flatten | join("\n")'
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.