从 Json 文件获取最近几分钟创建的值

发布于 2025-01-10 06:24:56 字数 1099 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2025-01-17 06:24:56

您可以检查 jq 中的时间,但它仅处理 ISO 8601 日期,没有毫秒。因此你必须将它们剪掉以进行比较。 now 为您提供当前时间。

jq '.[] | select(.updated_at | "\(.[:-5])Z" | fromdate + 900 > now).id'

如果你想有一个参数几分钟,请尝试:

jq --argjson min 15 '
  .[] | select(.updated_at | "\(.[:-5])Z" | fromdate + 60 * $min > now).id
'

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.

jq '.[] | select(.updated_at | "\(.[:-5])Z" | fromdate + 900 > now).id'

If you want to have a parameter for minutes, try:

jq --argjson min 15 '
  .[] | select(.updated_at | "\(.[:-5])Z" | fromdate + 60 * $min > now).id
'
乙白 2025-01-17 06:24:56
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

只需删除“$TIMEE”周围的引号即可。如果您引用它,它可能会被视为字符串。

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

just removing the quote around "$TIMEE" will work. It might be taken as a string if you quote it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文