3

I have a variable in bash that is an output of Amazon EMR create command;

CLUSTER=$(aws emr create-cluster ...)
echo $CLUSTER

the output is like this:

{ "ClusterId": "j-9YWMBYN98LN7" }

What I need to do is to extract the value j-9YWMBYN98LN7 to a new variable, something like:

ID=$CLUSTER.(ClusterId)

Of course the above command doesn't work. I have tried with jq but no luck.

ID=$(jq -r '.ClusterId' $CLUSTER)

The thing is I'm not even sure what type $CLUSTER is. How do I extract the value j-9YWMBYN98LN7 there? Thanks

2
  • if might not matter, but did you try ID=$(jq -r '.ClusterId' "$CLUSTER") ? (dbl-quoting CLUSTER var). Good luck. Commented May 5, 2016 at 2:34
  • Doesn't work either, here is the output: jq: { "ClusterId": "j-1FZMHAPPDD0LG" }: No such file or directory. Thanks anyway Commented May 5, 2016 at 3:24

3 Answers 3

13

If you want to use jq, you can do it this way

 ID=`echo ${CLUSTER} | jq -r '.ClusterId'`
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but this doesn't work either. I think this $CLUSTER is not json (may be string, I don't know). Here is the output: cat: {: No such file or directory cat: "ClusterId":: No such file or directory cat: "j-1FZMHAPPDD0LG": No such file or directory cat: }: No such file or directory
My bad, it should be echo instead of cat. I corrected my answer and it should work now.
It works now, thanks a lot, but I can only upvote your answer as I have accepted the other one :)
@user3685928 You can (and should) accept this answer instead.
simple & works 👍✨
5

If

echo $CLUSTER

gives you

{ "ClusterId": "j-9YWMBYN98LN7" }

then

ID=$(awk  'BEGIN{FS="\""}{print $4}' <<< "${CLUSTER}")

should do it.

echo "$ID"
j-9YWMBYN98LN7

4 Comments

This works. Thanks a lot!
@user3685928 : You're welcome pal !
This solution works in bash, but not in others shells. This is my variation to ash: ID=$(echo $CLUSTER | awk 'BEGIN{FS="\""}{print $4}')
@JavierYáñez Cool, Thankfully the question is tagged bash
2

Try the following:

ID=$(echo "${CLUSTER}" | sed 's/{ "ClusterId": "//' | sed 's/" }//')

That will isolate j-9YWMBYN98LN7, or whatever is between the last set of quotes, into the variable $ID.

I get the feeling there is more to what you need as you are probably planning on using the contents of $ID for something and there may be more efficient ways to capture that value for you. Maybe if you can explain a bit more how you plan on using the value we can provide even more actionable answers.

3 Comments

I'm writing a script to automate the process of creating an EMR cluster and adding jobs to it. I need this Cluster ID in the job-adding step like this: aws emr add-steps --cluster-id $ID. Again, I'm not sure what type of the variable $CLUSTER here, and I understand that just by giving you guys the output it could be hard
Anyway, the output of your command is still the same as $CLUSTER { "ClusterId": "j-1FZMHAPPDD0LG" }
It is odd that is did not work for you. You can check it via echo '{ "ClusterId": "j-9YWMBYN98LN7" }' | sed 's/{ "ClusterId": "//' | sed 's/" }//' For me it did indeed output j-9YWMBYN98LN7. No worries though, I like @sjsam 's awk answer better as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.