1
  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"
shellter
  • 36,525
  • 7
  • 83
  • 90
hpmistry19
  • 33
  • 7

4 Answers4

4

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.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I'd like to +10 just for the recommendation to stop using ALLCAPS. I think all shell related answers should contain that advice until that practice is purged from the world – William Pursell Oct 03 '20 at 13:02
  • Is there an 'official' source of the recommendation of not using ALLCAPS ? We see many examples of using it, like ORACLE_HOME, JAVA_HOME, or DOCKER_HOST, which is much more recent than the first two. – Philippe Oct 03 '20 at 17:04
  • I suppose I should say "don't use allcaps _local_ or _script_ variables". Conventionally, environment variables are allcaps. And I would not expect there to be an official source: it's not up to the GNU bash maintainers to tell programmers not to make mistakes. – glenn jackman Oct 03 '20 at 17:05
  • That makes much more sense. But if we look at `configure` script of bash source, we can see loads of ALLCAPS local or script variables, like PACKAGE_NAME, PACKAGE_VERSION, ... How can we explain it ? – Philippe Oct 03 '20 at 17:19
0

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)

YLR
  • 1,503
  • 4
  • 21
  • 28
0

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.

0

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(", ")')
Philippe
  • 20,025
  • 2
  • 23
  • 32