【问题标题】:jq --raw-output still producing quotes, cannot convert values to stringjq --raw-output 仍然产生引号,无法将值转换为字符串
【发布时间】:2020-04-09 16:58:03
【问题描述】:

我正在使用 -r 参数调用 jq,但在结果中,字符串是用引号引起来的。我怎样才能摆脱引号?

(我希望所有内容都用引号或不带引号)

jq -r '.[][] | [.name, .allocatedVCores, .runningContainers, .allocatedVcoreSeconds, .allocatedMemorySeconds]'

[
  "TestCase1_2a4e36be647a6abaf65c48fd1d3c8300",
  1,
  1,
  86708,
  88789601
]
[
  "TestCase2_2a6a14ec8365c4836bafd3fdbe647a00",
  239,
  239,
  3531555,
  5416951035
]
...

如果我打电话给| .join("`"),我会得到jq: error (at <stdin>:87): string ("`") and number (1) cannot be added

Ok 字符串和数字无法添加。这是有道理的。但我无法将所有内容都转换为字符串。 我尝试使用| tostring | .join("`") 对其进行转换,但这不起作用。

所以我尝试单独转换每个值

jq -r '.[][] | [ .name|tostring, .allocatedVCores|tostring, .runningContainers|tostring, .allocatedVcoreSeconds|tostring, .allocatedMemorySeconds|tostring] | join("`")'

但是突然 jq 无法识别其中一个键。 它可以过滤allocatedMemorySeconds 或allocatedVCores。如果我同时使用两者,我会得到。

jq: error (at <stdin>:87): Cannot index string with string "allocatedMemorySeconds"

此键存在,并且无需转换为字符串即可完美运行。我不知道为什么会这样。

我正在努力实现这个输出

`TestCase1_2a4e36be647a6abaf65c48fd1d3c8300`1`1`86708`88789601`
`TestCase2_2a6a14ec8365c4836bafd3fdbe647a00`239`239`3531555`5416951035`

基本上只是将一些 json 转换为“csv”,并使用反引号作为分隔符。 我正在使用 jq 1.5 版。知道如何实现这一目标吗?

输入数据

{
  "applications" : [ {
    "applicationId" : "application_1558485728047_0016",
    "name" : "TestCase1_2a4e36be647a6abaf65c48fd1d3c8300",
    "startTime" : "2020-04-08T15:49:30.886Z",
    "user" : "yyyyyyyyyyyyyyy",
    "pool" : "root.users.yyyyyyyy",
    "state" : "RUNNING",
    "progress" : 10.0,
    "attributes" : { },
    "applicationTags" : [ "" ],
    "allocatedMemorySeconds" : 88789601,
    "allocatedVcoreSeconds" : 86708,
    "allocatedMB" : 1024,
    "allocatedVCores" : 1,
    "runningContainers" : 1,
    "mr2AppInformation" : { }
  }, {
    "applicationId" : "application_1558480857247_0015",
    "name" : "TestCase2_2a6a14ec8365c4836bafd3fdbe647a00",
    "startTime" : "2020-04-08T10:43:58.924Z",
    "endTime" : "2020-04-08T15:21:32.374Z",
    "user" : "xxxxxxx",
    "pool" : "root.users.xxxxxxx",
    "state" : "FINISHED",
    "progress" : 100.0,
    "attributes" : { },
    "applicationTags" : [ "" ],
    "allocatedMemorySeconds" : 5416951035,
    "allocatedVcoreSeconds" : 3531555,
    "allocatedMB" : 366592,
    "allocatedVCores" : 239,
    "runningContainers" : 239,
    "mr2AppInformation" : { }
  } ],
  "warnings" : [ ]
}

【问题讨论】:

标签: jq


【解决方案1】:

该问题要求删除引号,但随后您说“我想要引号中的所有内容或没有引号”,然后要求所有内容用反引号分隔并提供示例输出。所以我不能 100% 确定你想要什么,但是就这样吧。

首先,所有内容都用引号引起来

$ jq '.[][] | [.name, .allocatedVCores, .runningContainers, .allocatedVcoreSeconds, .allocatedMemorySeconds] | (..|numbers) |= tostring' /tmp/data.json

[
  "TestCase1_2a4e36be647a6abaf65c48fd1d3c8300",
  "1",
  "1",
  "86708",
  "88789601"
]
[
  "TestCase2_2a6a14ec8365c4836bafd3fdbe647a00",
  "239",
  "239",
  "3531555",
  "5416951035"
]

重要的部分是末尾的| (..|numbers) |= tostring 位。它递归地查找所有数字并将它们转换为字符串。

现在,要获得带反引号的输出,试试这个野兽

$ jq -r '.[][] | [.name, .allocatedVCores, .runningContainers, .allocatedVcoreSeconds, .allocatedMemorySeconds] | (..|numbers) |= tostring | reduce .[] as $i (null; (.//"") + (if . == null then $i else "`" + $i end))//"" | ("`" + . + "`")'

`TestCase1_2a4e36be647a6abaf65c48fd1d3c8300`1`1`86708`88789601`
`TestCase2_2a6a14ec8365c4836bafd3fdbe647a00`239`239`3531555`5416951035`

这会从当前 jq 中窃取join 的定义,上面的评论者链接到该定义,因此您可以将字符串与当前版本连接。然后在最后将整行用反引号括起来;否则你不会在开头和结尾得到它们。

顺便说一句,我在 1.5 上使用 join 尝试了这个命令,效果很好:

$ jq -r '.[][] | [.name, .allocatedVCores, .runningContainers, .allocatedVcoreSeconds, .allocatedMemorySeconds] | (..|numbers) |= tostring | join("`") | ("`" + . + "`")'

`TestCase1_2a4e36be647a6abaf65c48fd1d3c8300`1`1`86708`88789601`
`TestCase2_2a6a14ec8365c4836bafd3fdbe647a00`239`239`3531555`5416951035`

但是你提到join 有问题,所以我想提供一个没有它的选项。

希望其中之一会有所帮助。祝你好运!

【讨论】:

  • 我最终得到了jq -r '.[][] | [.name, .applicationId, .startTime, .endTime, .user, .pool, .state, .allocatedMemorySeconds, .allocatedVcoreSeconds ] | (..|numbers) |= tostring | join("`")' 您对| (..|numbers) |= tostring 的想法似乎运作良好。谢谢!
  • @jmt 不错!很高兴我能帮忙
猜你喜欢
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 2022-08-02
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多