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

user3685928
  • 103
  • 2
  • 10
  • if might not matter, but did you try `ID=$(jq -r '.ClusterId' "$CLUSTER")` ? (dbl-quoting CLUSTER var). Good luck. – shellter May 05 '16 at 02:34
  • Doesn't work either, here is the output: `jq: { "ClusterId": "j-1FZMHAPPDD0LG" }: No such file or directory.` Thanks anyway – user3685928 May 05 '16 at 03:24

3 Answers3

13

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

 ID=`echo ${CLUSTER} | jq -r '.ClusterId'`
CS Pei
  • 10,869
  • 1
  • 27
  • 46
5

If

echo $CLUSTER

gives you

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

then

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

should do it.

echo "$ID"
j-9YWMBYN98LN7
sjsam
  • 21,411
  • 5
  • 55
  • 102
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.

John Mark Mitchell
  • 4,522
  • 4
  • 28
  • 29
  • 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 – user3685928 May 05 '16 at 03:32
  • Anyway, the output of your command is still the same as $CLUSTER `{ "ClusterId": "j-1FZMHAPPDD0LG" }` – user3685928 May 05 '16 at 03:33
  • 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. – John Mark Mitchell May 06 '16 at 04:12