My messages generator output
$ ./messages.sh
{"a":"v1"}
{"b":"v2"}
{"c":"v3"}
...
Required output
$ ./messages.sh | jq xxxxxx
[{"a":"v1"},{"b":"v2"}]
[{"c":"v3"},{"d":"v4"}]
...
My messages generator output
$ ./messages.sh
{"a":"v1"}
{"b":"v2"}
{"c":"v3"}
...
Required output
$ ./messages.sh | jq xxxxxx
[{"a":"v1"},{"b":"v2"}]
[{"c":"v3"},{"d":"v4"}]
...
3
Answers
Take the first item using
.
, and the second usinginput
(prepended bytry
to handle cases of not enough input items). Then, wrap them both into array brackets, and provide the-c
option for compact output. jq will work through its whole input one by one (or two by two).You can surround the array body with
limit
, and useinputs
instead (note thes
) to fetch more than just one item:Use
slurp
with_nwise(2)
to chunk into parts of 2:The
--compact-output
is to output each array on a single lineHere is a stream-oriented, generic and portable def of nwise:
For the task at hand, you could use it like so:
with the -n command-line option.