用一个衬垫将时间戳替换为文件中的当前时间

发布于 2025-01-09 18:20:00 字数 252 浏览 0 评论 0 原文

我有一个包含时间戳的文件:

"buildTimestamp": "2021-07-19T17:00:00Z"

我想使用一行命令将其替换为当前的构建时间。

date | xargs -I {} perl -pi -e 's/2021-07-19T17:00:00Z/"$0"/g' serviceProperties.json 

但它没有按预期工作。有什么建议吗?

I have a file which contains a timestamp:

"buildTimestamp": "2021-07-19T17:00:00Z"

I want to replace it with the current build time, using a one-line command.

date | xargs -I {} perl -pi -e 's/2021-07-19T17:00:00Z/"$0"/g' serviceProperties.json 

But it's not working as expected. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

初懵 2025-01-16 18:20:00

您确实应该使用 JSON 解析器来处理 JSON 数据。 可以完成这项工作。

如果您的文件看起来像

{
  "foo": {
    "bar": {
      "buildTimestamp": "2021-07-19T17:00:00Z"
    }
  }
}

然后

jq '.foo.bar.buildTimestamp = (now | strftime("%FT%TZ"))' serviceProperties.json

在 2022 年 2 月 24 日 16:36:15 America/New_York 输出此内容

{
  "foo": {
    "bar": {
      "buildTimestamp": "2022-02-24T21:36:15Z"
    }
  }
}

如果您不能倡导 jq 至少获取 JSON 混合中的 perl 模块:

perl -MJSON -MPOSIX=strftime -0777 -nE '
    $json = JSON->new();
    $data = $json->decode($_);
    $data->{foo}{bar}{buildTimestamp} = strftime("%FT%TZ", gmtime);
    say $json->pretty->encode($data);
' serviceProperties.json
{
   "foo" : {
      "bar" : {
         "buildTimestamp" : "2022-02-25T14:41:04Z"
      }
   }
}

You should really be using a JSON parser to handle JSON data. can do this job.

If your file looks like

{
  "foo": {
    "bar": {
      "buildTimestamp": "2021-07-19T17:00:00Z"
    }
  }
}

Then

jq '.foo.bar.buildTimestamp = (now | strftime("%FT%TZ"))' serviceProperties.json

outputs this on Feb 24, 2022 at 16:36:15 America/New_York

{
  "foo": {
    "bar": {
      "buildTimestamp": "2022-02-24T21:36:15Z"
    }
  }
}

If you can't advocate for jq at least get the JSON perl module in the mix:

perl -MJSON -MPOSIX=strftime -0777 -nE '
    $json = JSON->new();
    $data = $json->decode($_);
    $data->{foo}{bar}{buildTimestamp} = strftime("%FT%TZ", gmtime);
    say $json->pretty->encode($data);
' serviceProperties.json
{
   "foo" : {
      "bar" : {
         "buildTimestamp" : "2022-02-25T14:41:04Z"
      }
   }
}
乖乖哒 2025-01-16 18:20:00

假设 serviceProperties.json 中的数据

[
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    },
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    },
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    },
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    }
]

运行后 date +%FT%TZ | xargs -I % perl -pi -e 's/2021-07-19T17:00:00Z/%/g' serviceProperties.json serviceProperties.json

将是

[
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    },
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    },
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    },
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    }
]

当您替换为 $0 时,它将采用来自 perl 的参数命令,因此你会得到意想不到的结果。相反,使用 xargs 替换为助手就可以解决问题。

要了解有关 xargs 在这种情况下如何工作的更多信息 阅读此处

Assuming data in serviceProperties.json

[
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    },
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    },
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    },
    {
        "buildTimestamp": "2021-07-19T17:00:00Z"
    }
]

After running date +%FT%TZ | xargs -I % perl -pi -e 's/2021-07-19T17:00:00Z/%/g' serviceProperties.json

serviceProperties.json will be

[
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    },
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    },
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    },
    {
        "buildTimestamp": "2022-02-25T03:30:20Z"
    }
]

As you are replacing with $0 it will take arguments from perl command and hence you will have unexpected results. Instead replacing with a helper using xargs will do the trick.

To learn more about how xargs is working in this case read here

素罗衫 2025-01-16 18:20:00
perl -pi -e 's/2021-07-19T17:00:00Z/'"$(date)"'/g' serviceProperties.json 
perl -pi -e 's/2021-07-19T17:00:00Z/'"$(date)"'/g' serviceProperties.json 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文