【发布时间】: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" : [ ]
}
【问题讨论】:
-
joinsupports numbers and booleans since 2015。你真的应该更新你的 jq。
标签: jq