【发布时间】: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 库