从 Json 文件获取最近几分钟创建的值
我的 Json 文件包含如下数据:
[
{
"id": 47,
"iid": 12,
"project_id": 1,
"status": "pending",
"source": "push",
"ref": "new-pipeline",
"sha": "ab23456789d",
"web_url": "https://example.com/project/pipelines/47",
"created_at": "2022-02-24T11:28:34.085Z",
"updated_at": "2016-08-24T15:32:35.169Z"
},
{
"id": 48,
"iid": 13,
"project_id": 1,
"status": "pending",
"source": "web",
"ref": "new-pipeline",
"sha": "ab23456789d",
"web_url": "https://example.com/project/pipelines/48",
"created_at": "2022-02-23T11:28:34.085Z",
"updated_at": "2016-08-23T15:32:35.169Z"
}
]
我正在尝试获取最近 15 分钟内创建的 ID。但我无法得到它。
我已经尝试过以下方法,
TIM=`date -u +"%Y-%m-%dT%H:%M:%S.000Z" -d '-15 minutes'`
jq -r --arg TIMEE "$TIM" '.[]|select((.ref|contains("dev")) and (.updated_at >= "$TIMEE"))|.id' MyJsonFile.json
但这并没有按预期工作。我没有看到任何 ID。但当条件设为 (.updated_at >= "$TIMEE")
时。即使在最后一分钟我也可以看到创建的所有 ID。
不确定我是否以正确的方式尝试。非常感谢任何帮助。
My Json file has the data like below:
[
{
"id": 47,
"iid": 12,
"project_id": 1,
"status": "pending",
"source": "push",
"ref": "new-pipeline",
"sha": "ab23456789d",
"web_url": "https://example.com/project/pipelines/47",
"created_at": "2022-02-24T11:28:34.085Z",
"updated_at": "2016-08-24T15:32:35.169Z"
},
{
"id": 48,
"iid": 13,
"project_id": 1,
"status": "pending",
"source": "web",
"ref": "new-pipeline",
"sha": "ab23456789d",
"web_url": "https://example.com/project/pipelines/48",
"created_at": "2022-02-23T11:28:34.085Z",
"updated_at": "2016-08-23T15:32:35.169Z"
}
]
I am trying to fetch the IDs which are created in last 15 minutes. But I couldnt get it.
I have tried the below way,
TIM=`date -u +"%Y-%m-%dT%H:%M:%S.000Z" -d '-15 minutes'`
jq -r --arg TIMEE "$TIM" '.[]|select((.ref|contains("dev")) and (.updated_at >= "$TIMEE"))|.id' MyJsonFile.json
But this is not working as expected. I dont see any IDs. But when made the condition to (.updated_at >= "$TIMEE")
. I can see all IDs which are created even in last one minute.
Not sure if I am trying in the right way. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查 jq 中的时间,但它仅处理 ISO 8601 日期,没有毫秒。因此你必须将它们剪掉以进行比较。
now
为您提供当前时间。如果你想有一个参数几分钟,请尝试:
You can check the time within jq, but it handles ISO 8601 dates only without milliseconds. Therefore you have to cut them off for comparison.
now
gives you the current time.If you want to have a parameter for minutes, try:
只需删除“$TIMEE”周围的引号即可。如果您引用它,它可能会被视为字符串。
just removing the quote around "$TIMEE" will work. It might be taken as a string if you quote it.