我们有以下json
( example 1
)
示例1
more file.json
{
"version": 1,
"partitions": [
{
"topic": "list_of_cars",
"partition": 2,
"replicas": [
1003,
1004,
1005
],
"log_dirs": [
"any",
"any",
"any"
]
},
{
"topic": "list_of_cars",
"partition": 4,
"replicas": [
1005,
1006,
1001
],
"log_dirs": [
"any",
"any",
"any"
]
},
{
"topic": "list_of_cars",
"partition": 0,
"replicas": [
1001,
1002,
1003
],
"log_dirs": [
"any",
"any",
"any"
]
},
{
"topic": "list_of_cars",
"partition": 1,
"replicas": [
1002,
1003,
1004
],
"log_dirs": [
"any",
"any",
"any"
]
},
{
"topic": "list_of_cars",
"partition": 5,
"replicas": [
1006,
1001,
1002
],
"log_dirs": [
"any",
"any",
"any"
]
},
{
"topic": "list_of_cars",
"partition": 3,
"replicas": [
1004,
1005,
1006
],
"log_dirs": [
"any",
"any",
"any"
]
}
]
}
我们想要从topic
(示例 1)中剪切每个数组
并将其重定向到json文件(如下file1.json file2.json file3.json ..
所示)
第一个文件
more file1.json
{
"version": 1,
"partitions": [{
"topic": "list_of_cars",
"partition": 2,
"replicas": [
1003,
1004,
1005
],
"log_dirs": [
"any",
"any",
"any"
]
}]
}
第二个文件
more file2.json
{
"version": 1,
"partitions": [{
"topic": "list_of_cars",
"partition": 4,
"replicas": [
1005,
1006,
1001
],
"log_dirs": [
"any",
"any",
"any"
]
}]
}
第三个文件
more file3.json
.
.
.
答案1
由于.partition[].partition
似乎保存了任何特定分区的标识符,因此我们可以循环这些分区,并针对每个标识符删除不具有该特定标识符的分区。
以下假设标识符是一个不带空格等的简单整数:
for part in $(jq '.partitions[].partition' file.json); do
jq --argjson part "$part" 'del(.partitions[] | select( .partition != $part ))' file.json >file-partition-"$part".json
done
这将为每个分区创建一个文件,以分区命名,而不是数组中的索引.partition[]
。