我有一个巨大的json文件,其密钥以“ /”开头。我想基于键名创建多个json文件。
/upgrade-coordinator/api/v1/upgrade/eula/acceptance
/upgrade-coordinator/api/v1/upgrade/history
/upgrade-coordinator/api/v1/upgrade/nodes
/upgrade-coordinator/api/v1/upgrade/nodes-summary
/upgrade-coordinator/api/v1/upgrade/status-summary
/upgrade-coordinator/api/v1/upgrade/summary
/upgrade-coordinator/api/v1/upgrade/upgrade-unit-groups
/upgrade-coordinator/api/v1/upgrade/upgrade-unit-groups-status
在网站上出现一些注释后,我发现:
for f in `cat input.json | jq -r 'keys[]'` ; do
cat input.json | jq ".$f" > $f.json
done
or when you insist on more bashy syntax like some seem to prefer:
for f in $(jq -r 'keys[]') ; do
jq ".[\"$f\"]" < input.json > "$f.json"
done < input.json
尝试上述操作时,出现错误:
-bash: -/nsxapi/api/v1/vpn/l2vpn/sessions/summary.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/eula/acceptance.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/history.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/nodes.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/nodes-summary.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/status-summary.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/summary.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/upgrade-unit-groups.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/upgrade-unit-groups-status.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/upgrade-unit-groups/aggregate-info.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/upgrade-units.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/upgrade-units-stats.json: No such file or directory
-bash: -/upgrade-coordinator/api/v1/upgrade/upgrade-units/aggregate-info.json: No such file or directory
我认为它可能是bcos,它希望在路径-/ upgrade-coordinator / api / v1 / upgrade / upgrade-units /中创建gregation-info.json。如果这是正确的,我该如何替换第一个空格中的每个键中的字符串“ /”?请给我一些帮助吗?
答案 0 :(得分:0)
这意味着-以第一个错误消息为例-,目录-/nsxapi/api/v1/vpn/l2vpn/sessions
不存在。您可以通过
ls -d -- -/nsxapi/api/v1/vpn/l2vpn/sessions
在运行脚本的工作目录中。
请注意,重定向到某些路径不会自动创建间歇性目录。