The strptime()
function is used to parse a formatted time (the inverse of what strftime()
does). You want strftime()
.
Using jq
:
jq -n 'now as $t | { date: [ ($t|strftime("%H")), ($t|strftime("%M")), ($t|strftime("%S")) ] }'
This uses strftime()
to format the time given by the now
function. Use strflocaltime()
to convert the time to local time.
If that's too wordy or seems inefficient (we're actually calling strftime()
once for each element of the date
array!), then consider creating a HH:MM:SS
timestamp string, and then splitting it on the colons:
jq -n '{ date: (now | strftime("%H:%M:%S") | split(":")) }'
Either of the above commands may produce
{
"date": [
"07",
"01",
"23"
]
}
Complete script (uses jo
to safely create the initial vtg
array from the values of a named bash
array):
#!/bin/bash
value=(11.769 11.769 11.339 11.788 11.867 11.886 11.808 11.339 11.554 11.182)
jo vtg="$( jo -a "${value[@]}" )" |
jq '. += now as $t | { date: [ ($t|strftime("%H")), ($t|strftime("%M")), ($t|strftime("%S")) ] }'
# or...
jo vtg="$( jo -a "${value[@]}" )" |
jq '. += { date: (now | strftime("%H:%M:%S") | split(":")) }'
You could obviously also create the date section of the data on the fly with jo
reading from the date
command:
jo vtg="$( jo -a "${value[@]}" )" \
date="$( jo -a $( date +'%H %M %S' ) )"
This relies on the shell splitting the output from date
on the spaces in the format string. You will get the local time from the above.
The jo
tool is available from https://jpmens.net/2016/03/05/a-shell-command-to-create-json-jo/
Assuming that the data in your file should be taken as a raw string, it would need to be JSON-encoded. It is easiest to do that with a program that understands JSON, like jq
:
json='
{
"actions": [
{
"data": { "from": "test", "message": "", "to": "test2" },
"name": "transfer"
}
],
"sec": 0,
"usage": 0
}
'
json=$( jq --arg value "$(cat "$file")" '.actions[0].data.value = $value' <<<"$json" )
cos -u 'https://myapi.com' push data "$json"
Note that $value
in the jq
expression is not a shell variable but an internal jq
variable whose value is given on the command line with --arg
and that it's automatically JSON-encoded.
A slightly more convenient way to handle the data in $file
without having to expand it on the command line (which would matter if the amount of data is large) is to convert the data to a JSON string separately and pass it to the jq
that inserts it in the correct place via a process substitution:
jq '.actions[0].data.value = input' - <( jq -Rs . data ) <<<"$json"
You could also use jo
to create the data
object with the value
key's value read from the $file
file and then just insert that with a shell variable:
datajson=$( jo from=test message="" to="test2" value=@"$file" )
json='
{
"actions": [
{
"data": '"$datajson"',
"name": "transfer"
}
],
"sec": 0,
"usage": 0
}
'
cos -u 'https://myapi.com' push data "$json"
Note how we temporarily break out of the single-quoted string that makes up the JSON document to insert the quoted expansion of $datajson
.
Best Answer
To delete the
.packages.code
key and its value, usingjq
:To delete any entry under
.packages
whose.name
key has the valuecode
:The same two commands, but they take the
code
string from a shell variable:Redirect the output to a new file and replace the old file with that if it looks ok.