jq function pad
I frequently format jq output in a one line per item string with | separators. This makes it easy to copy/paste into excel or contruct a markdown table. Here is an example of the same formatting using a jq def function. Ie the lpad and rpad functions in the code below.
#!/bin/bash
endpoint="http://192.168.1.10:12321/desktop"
if [ -n "$1" ]; then
echo "get # of links: $1"
SQL="SELECT link_date, link_text, link_url FROM links ORDER BY link_date DESC LIMIT ${1}"
else
echo "No # of links then show all"
SQL="SELECT link_date, link_text, link_url FROM links ORDER BY link_date DESC"
fi
### sqlite3 web service
transaction=$(cat <<EOT
{
"transaction": [
{
"query": "$SQL"
}
]
}
EOT
)
response=$(curl -s X POST -H "Content-Type: application/json" -d "$transaction" ${endpoint} | jq -r '.results[].resultSet[]')
echo $response | jq -r '
def lpad($len; $char): tostring | ($char * ([$len - length, 0] | max)) + .;
def rpad($len; $char): tostring | . + ($char * ([$len - length, 0] | max));
"\(.link_date) | \(.link_text | rpad(40; " ")) | \(.link_url)"
'
NOTE: included the whole script since I re-use this sqlite webservice pattern a lot