How to use printf in bash?

Need help with printf in bash.

There is such script:
#!/bin/bash

VHOSTNAME=/
NODENAME=rabbit1

template='{"{#VHOSTNAME}": "%s", "{#QUEUENAME}": "%s", "{#NODENAME}": "%s"}, '

cat test.txt | while read LINE; do printf "$template\n" "$VHOSTNAME" "$LINE" "$NODENAME";done


The contents of the file test.txt:
rabbit1-test1
rabbit1-test2
rabbit1-test3


The output will be:
{"{#VHOSTNAME}": "/", "{#QUEUENAME}": "rabbit1-test1", "{#NODENAME}": "rabbit1"}, 
{"{#VHOSTNAME}": "/", "{#QUEUENAME}": "rabbit1-test2", "{#NODENAME}": "rabbit1"}, 
{"{#VHOSTNAME}": "/", "{#QUEUENAME}": "rabbit1-test3", "{#NODENAME}": "rabbit1"},


As in the beginning of the conclusion to add the following text:
{"data": [

So now we have:
{"data": [
{"{#VHOSTNAME}": "/", "{#QUEUENAME}": "rabbit1-test1", "{#NODENAME}": "rabbit1"}, 
{"{#VHOSTNAME}": "/", "{#QUEUENAME}": "rabbit1-test2", "{#NODENAME}": "rabbit1"}, 
{"{#VHOSTNAME}": "/", "{#QUEUENAME}": "rabbit1-test3", "{#NODENAME}": "rabbit1"},
April 7th 20 at 15:39
2 answers
April 7th 20 at 15:41
Solution
here was an example of a script creating a csv metrics json type of the desired structure
DATAFILE1="/tmp/data1.$$"
# NOTE: do not use | as a separator
cat<<EOF>$DATAFILE1
first:11 second:12:third:13
first:22 second:22:third:23
first:32 second:32:third:33
EOF

1>&2 echo 'Loading:'
1>&2 cat $DATAFILE1
1>&2 echo '---'

DATAFILE2="/tmp/data2.$$"
DATAFILE2="/tmp/data.tmp.json"

IFS=':'; cat $DATAFILE1| while read KEY1 VALUE1 KEY2 VALUE2 KEY3 VALUE3; do
jq --arg k1 "$KEY1" \
 v1 --arg "$VALUE1" \
 k2 --arg "$KEY2" \
 v2 --arg "$VALUE2" \
 k3 --arg "$KEY3" \
 v3 --arg "$VALUE3" \
 '. | .[$k1]=$v1 | .[$k2]=$v2 | .[$k3]=$v3' \
 <<<'{}' ;
done > $DATAFILE2
DATA_KEY='data'
# making the rowset keyed by $DATA_KEY
# TODO: explore alternatives
cat $DATAFILE2 | jq --slurp '.' | jq "{\"$DATA_KEY\": .}"

run
it gives

Loading:
first:11 second:12:third:13
first:22 second:22:third:23
first:32 second:32:third:33
---

{
 "data": [
{
 "first": "11",
 "second": "12",
 "third": "13"
},
{
 "first": "22",
 "second": "22",
 "third": "23"
},
{
 "first": "32",
 "second": "32",
 "third": "33"
}
]
}
April 7th 20 at 15:43
man 1 printf, info printf to read tried?

Find more questions by tags bash