【问题标题】:jq convert timezone in bash scriptjq在bash脚本中转换时区
【发布时间】:2020-05-04 07:54:01
【问题描述】:

我有一个查询如下:

dump=$(jq -j --raw-output '.aggregations | ."2" | .buckets[] | "\(.key),",
                                       (."5" | .buckets[] | "\(.key),",
                                       (."3" | .buckets[] | "\(.key),",
                                       (."4" | .buckets[] | "\(.key),",
                                       (."8" | .buckets[] | "\(.key),",
                                       (."6" | .buckets[] | "\(.key),",
                                       (."7" | .buckets[] | "\(.key),",
                                       (."9" | .buckets[] | "\(.key),",
                                       (."10" | .buckets[] | "\(.key),",
                                       (."11" | .buckets[] | "\(.key),",
                                       (."12" | .buckets[] | "\(.key),",
                                       (."13" | .buckets[] | "\(.key),",
                                       (."14" | .buckets[] | "\(.key),",
                                       (."15" | .buckets[] | "\(.key),",
                                       (."1" | ."hits" | .hits[] | ."_source" |
                                           "\(."@timestamp")" | "\n"))))))))))))))' <<< "$cl_report")
echo -e "${dump}" >> report.csv

时间戳为 ISO8601 格式。

我想将 时间戳 从 UTC 转换为马德里时间。

我想要的是这个

dump=$(jq -j --raw-output '.aggregations | ."2" | .buckets[] | "\(.key),",
                                       (."5" | .buckets[] | "\(.key),",
                                       (."3" | .buckets[] | "\(.key),",
                                       (."4" | .buckets[] | "\(.key),",
                                       (."8" | .buckets[] | "\(.key),",
                                       (."6" | .buckets[] | "\(.key),",
                                       (."7" | .buckets[] | "\(.key),",
                                       (."9" | .buckets[] | "\(.key),",
                                       (."10" | .buckets[] | "\(.key),",
                                       (."11" | .buckets[] | "\(.key),",
                                       (."12" | .buckets[] | "\(.key),",
                                       (."13" | .buckets[] | "\(.key),",
                                       (."14" | .buckets[] | "\(.key),",
                                       (."15" | .buckets[] | "\(.key),",
                                       (."1" | ."hits" | .hits[] | ."_source" | "\ 
                                       (."@timestamp") as $tstamp | "TZ Europe/Madrid" date -d $tstamp" | \n"))))))))))))))' <<< "$cl_report")

Bash 抛出错误,抱怨 " 和 ' 等。

进行这种转换的正确方法是什么?

好的。 jq 1.5似乎有一个错误。此版本中 strftime 等无法正确读取 TZ。

jq 1.6 可以提供帮助:

report_dump=$(TZ=Erope/Madrid jq1.6 -j --raw-output '.aggregations | ."2" | .buckets[] | "\(.key),",
                                       (."5" | .buckets[] | "\(.key),",
                                       (."3" | .buckets[] | "\(.key),",
                                       (."4" | .buckets[] | "\(.key),",
                                       (."8" | .buckets[] | "\(.key),",
                                       (."6" | .buckets[] | "\(.key),",
                                       (."7" | .buckets[] | "\(.key),",
                                       (."9" | .buckets[] | "\(.key),",
                                       (."10" | .buckets[] | "\(.key),",
                                       (."11" | .buckets[] | "\(.key),",
                                       (."12" | .buckets[] | "\(.key),",
                                       (."13" | .buckets[] | "\(.key),",
                                       (."14" | .buckets[] | "\(.key),",
                                       (."15" | .buckets[] | "\(.key),",
                                       (."1" | ."hits" | .hits[] | ."_source" | .["@timestamp"] | sub(".[0-9]+Z$"; "Z") | fromdateiso8601 | strflocaltime("%Y-%m-%dT%H:%M:%S %Z"), "\n"))))))))))))))' <<< "$cl_report")

如上所示,我将剥离的时间戳通过管道传输到 fromdateiso8601 以获取秒数,然后通过管道传输到利用 TZ 获取本地时间的 strflocaltime。

【问题讨论】:

  • 包括您的源 json 数据以及转换后的输出应该是什么样子。
  • 你确定你有正确的标签吗? [jquery] 是一个 javascript 库

标签: linux bash timezone jq


【解决方案1】:

不幸的是,jq 对本地时区的处理一直存在问题 您可能需要根据具体情况调整以下内容 您的 jq 版本(即版本号和平台)。

这是使用 TZ 环境变量的示例,例如

TZ=Europe/Madrid jq -n -f program.jq

program.jq 包含的位置:

{"@timestamp": "2020-05-04T18:02:13Z"}
| .["@timestamp"]
| fromdateiso8601   # converts to seconds
| gmtime
| strftime("%Y-%m-%dT%H:%M:%S%Z")

输出:"2020-05-04T19:02:13CET"

注意:%Z 变为 CET。

【讨论】:

  • 太棒了。我们快到了。除此之外,我的时间就像 {"@timestamp": "2020-05-03T22:41:34.237Z"}。那些毫秒 237 不允许时间戳的格式与 fromdateiso8601 输入相匹配。你如何解决这个问题?
  • 去掉它们,如果需要,再重新添加。
猜你喜欢
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多