hpmistry19
hpmistry19

Reputation: 33

Assign output values in one variable using separators

  RECORDS=$(aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID \
   | $JQ -r '.ResourceRecordSets[] | select (.Name == "ap.com.") | .Name')

The Output value from $RECORDS prints in the format shown below in separated lines.

>> echo "recordType: $RECORDS"
recordType: ap.com.
ap1.com.
ap2.com.

How to print output in the format as (in inverted commas separating by comma)

>> echo "recordType: $RECORDS"
recordType: "ap.com, ap1.com, ap2.com"

Upvotes: 1

Views: 64

Answers (4)

Philippe
Philippe

Reputation: 26457

You can just change slightly your jq command :

records=$(aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID \
   | $JQ -r '[.ResourceRecordSets[] | select (.Name == "ap.com.") | .Name]|join(", ")')

Upvotes: 0

tarek mohamed
tarek mohamed

Reputation: 11

You can use:

echo recordType:\"$RECORDS\"|sed 's/ /,/g'|sed 's/:/: /'

Where the first sed to replace the commas, the second one to add the one space after the colon.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246764

I would read the jq output into an array, and use bash's own ability to join things:

# redirect from a process substitution into mapfile 
mapfile -t records < <(aws ...| jq ...)

printf 'recordType: "%s"\n' "$(IFS=,; echo "${records[*]}")"

That joins with just a comma, not comma+space:

recordType: "ap.com.,ap1.com.,ap2.com."

Get out of the habit of using ALLCAPS variable names, leave those as reserved by the shell. One day you'll write PATH=something and then wonder why your script is broken.

Upvotes: 4

YLR
YLR

Reputation: 1540

Try this line :

echo "recordType: $RECORDS"|sed -e "s/\.$/\", /"|sed -e "s/ap/\"ap/"|tr -d '\r\n'| rev | cut -c 3- | rev

Not probably the most short and efficient but it works ;) (and it could be easily customize)

Upvotes: 0

Related Questions