我建议使用 jq 将数组拆分为您想要的 JSON 对象流(每行一个),然后使用其他工具(例如 awk)来填充文件。以下是第一部分的完成方式:
def splitup(n):
def _split:
if length == 0 then empty
else .[0:n], (.[n:] | _split)
end;
if n == 0 then empty elif n > 0 then _split else reverse|splitup(-n) end;
# For the sake of illustration:
def data: { results: [range(0,20)]};
data | .results | {results: splitup(5) }
调用:
$ jq -nc -f splitup.jq
{"results":[0,1,2,3,4]}
{"results":[5,6,7,8,9]}
{"results":[10,11,12,13,14]}
{"results":[15,16,17,18,19]}
对于第二部分,您可以(例如)将 jq 输出通过管道传输到:
awk '{ file="file."++n; print > file; close(file); }'
您可能感兴趣的一个变体是让 jq 过滤器在交替行上同时发出文件名和 JSON;然后 awk 脚本也会读取文件名。