将jq的输出组合到每个记录输出的单行中

时间:2020-09-30 11:09:29

标签: jq

我目前有jq的输出,如下所示:

1§
{"id":"1","name":"River Street , Clerkenwell","lastUpdate":"1601461560941"}
2§
{"id":"2","name":"Phillimore Gardens, Kensington","lastUpdate":"1601461560941"}

,我希望它加入每条记录的输出行,即:

1§{"id":"1","name":"River Street , Clerkenwell","lastUpdate":"1601461560941"}
2§{"id":"2","name":"Phillimore Gardens, Kensington","lastUpdate":"1601461560941"}

以下是示例输入和当前过滤器:https://jqplay.org/s/qBfGyriA5B

如果我使用-j,则所有内容都在同一行,这不是我想要的

1§{"id":"1","name":"River Street , Clerkenwell","lastUpdate":"1601461560941"}2§{"id":"2","name":"Phillimore Gardens, Kensington","lastUpdate":"1601461560941"}

1 个答案:

答案 0 :(得分:1)

一种方法是在创建的对象上通过空字符串使用join函数。请注意,您要创建的对象需要转换为字符串类型才能起作用。在--raw-output/-r模式下使用以下过滤器

.stations.station[] + { lastUpdate: .stations."@lastUpdate" } | 
[ .id + "§", tostring ] | 
join("")

jqplay demo