我正在运行curl rest-api调用,并尝试在UBUNTU中增加一些键/值对。这是我现在的命令:
curl ..... | jq -c '{"online": .switches.optional.online, "offline": .switches.optional.offline}'
我收到的输出如下:
{ "online": 85, "offline": 196 }
但我真正想要的是有现在的时间戳,包括json的身体,好像:
{ "current-time": "Wed Apr 15 14:18:42 PDT 2020", "online": 85, "offline": 196 }
API响应体没有当前的时间戳消息,这能由jq本身触发吗?
谢谢。
杰克
发布于 2020-04-15 13:51:31
应该可以将另一个命令(或变量等)的结果传递给jq命令。
如果在bash中,下面这样的内容可以工作:
curl ..... | jq -c --arg datum "$(date)" '{"online": .switches.optional.online, "offline": .switches.optional.offline, "current-time": $datum}'
在这里,我们将$(date)
命令的结果作为参数$datum传递给jq过滤器。
date命令可以给出以多种不同方式格式化的当前时间。
发布于 2020-04-15 13:38:40
jq有now
内置:
TZ=UTC jq -n 'now | strftime("%a %b %d, %Y %Z %H:%M:%S")'
"Wed Apr 15, 2020 UTC 21:51:07"
请注意,环境变量TZ将影响strftime
生成的字符串的%Z部分,但不影响数值时间部分:
TZ=Australia/Sydney jq -n 'now | strftime("%a %b %d, %Y %Z %H:%M:%S")'
"Wed Apr 15, 2020 AEST 21:52:19"
相反,jq和gojq (jq的Go实现)的strflocaltime
函数将显示相对于TZ的“本地时间”:
$ gojq -n 'now | strflocaltime("%a %b %d, %Y %Z %H:%M:%S")'
"Wed May 04, 2022 EDT 17:39:48"
$ TZ=Australia/Sydney gojq -n 'now | strflocaltime("%a %b %d, %Y %Z %H:%M:%S")'
"Thu May 05, 2022 AEST 07:40:00"
$ TZ=Australia/Sydney jq -n 'now | strflocaltime("%a %b %d, %Y %Z %H:%M:%S")'
"Thu May 05, 2022 AEST 07:40:00"
发布于 2020-04-15 14:04:17
假设卷曲的反应看起来跟我硬编码的一样..。
$ jq --arg now "$(date)" '{"current-time": $now, "online": .switches.optional.online, "offline": .switches.optional.offline}' <<<'{ "switches": {"optional": {"online": 85, "offline": 196 }}}'
{
"current-time": "Wed Apr 15 22:03:00 UTC 2020",
"online": 85,
"offline": 196
}
希望这能有所帮助!
https://stackoverflow.com/questions/61238933
复制相似问题