在shell脚本中提取json负载

在shell脚本中提取json负载,shell,Shell,我有一个像下面这样的文件。如您所见,花括号之间几乎没有行/内容。由于有多组打开和关闭的花括号,我想分别获取每行的花括号({和})之间的内容 示例文件: 预期结果:需要3个分开的数据块,位于花括号之间 有人能帮我吗?如果您有一系列有效的JSON对象,您可以使用jq轻松、可靠地处理它们: 给定文件.jsons: { "/tmp/©ƒ-4bf57ed2-velero/velero/templates/crds.yaml": [ "" ], "/tmp/velero-4bf57ed2-

我有一个像下面这样的文件。如您所见,花括号之间几乎没有行/内容。由于有多组打开和关闭的花括号,我想分别获取每行的花括号({和})之间的内容

示例文件: 预期结果:需要3个分开的数据块,位于花括号之间


有人能帮我吗?

如果您有一系列有效的JSON对象,您可以使用
jq
轻松、可靠地处理它们:

给定
文件.jsons

{
   "/tmp/©ƒ-4bf57ed2-velero/velero/templates/crds.yaml": [ ""
   ],
   "/tmp/velero-4bf57ed2-velero/velero/templates/deployment.yaml": [ ""
   ],
   "/tmp/velero-4bf57ed2-velero/velero/templates/restic-daemonset.yaml": [ ""
   ],
   "/tmp/velero-4bf57ed2-velero/velero/templates/secret.yaml": [ ""
   ]
 }
 {
   "/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/deployment.yaml": [
     ".spec.replicas: '2' != '0'"
   ],
   "/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/servicemonitor.yaml": [
     "error: the server doesn't have a resource type \"ServiceMonitor\"\n"
   ]
 }
 {
   "/tmp/metrics-server-1960953a-metrics-server-certs/raw/templates/resources.yaml": [
     "error: the server doesn't have a resource type \"Issuer\"\n",
     "error: the server doesn't have a resource type \"Certificate\"\n"
   ]
 }
例如,可以将每个对象重新格式化为一行:

$ jq -s -r 'map(@json) | join("\n")' < file.jsons 
{"/tmp/©ƒ-4bf57ed2-velero/velero/templates/crds.yaml":[""],"/tmp/velero-4bf57ed2-velero/velero/templates/deployment.yaml":[""],"/tmp/velero-4bf57ed2-velero/velero/templates/restic-daemonset.yaml":[""],"/tmp/velero-4bf57ed2-velero/velero/templates/secret.yaml":[""]}
{"/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/deployment.yaml":[".spec.replicas: '2' != '0'"],"/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/servicemonitor.yaml":["error: the server doesn't have a resource type \"ServiceMonitor\"\n"]}
{"/tmp/metrics-server-1960953a-metrics-server-certs/raw/templates/resources.yaml":["error: the server doesn't have a resource type \"Issuer\"\n","error: the server doesn't have a resource type \"Certificate\"\n"]}
$jq-s-r'map(@json)| join(“\n”)”

现在,您可以逐行处理它,而不必担心匹配大括号。

如果您有一系列有效的JSON对象,您可以使用
jq
轻松、可靠地处理它们:

给定
文件.jsons

{
   "/tmp/©ƒ-4bf57ed2-velero/velero/templates/crds.yaml": [ ""
   ],
   "/tmp/velero-4bf57ed2-velero/velero/templates/deployment.yaml": [ ""
   ],
   "/tmp/velero-4bf57ed2-velero/velero/templates/restic-daemonset.yaml": [ ""
   ],
   "/tmp/velero-4bf57ed2-velero/velero/templates/secret.yaml": [ ""
   ]
 }
 {
   "/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/deployment.yaml": [
     ".spec.replicas: '2' != '0'"
   ],
   "/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/servicemonitor.yaml": [
     "error: the server doesn't have a resource type \"ServiceMonitor\"\n"
   ]
 }
 {
   "/tmp/metrics-server-1960953a-metrics-server-certs/raw/templates/resources.yaml": [
     "error: the server doesn't have a resource type \"Issuer\"\n",
     "error: the server doesn't have a resource type \"Certificate\"\n"
   ]
 }
例如,可以将每个对象重新格式化为一行:

$ jq -s -r 'map(@json) | join("\n")' < file.jsons 
{"/tmp/©ƒ-4bf57ed2-velero/velero/templates/crds.yaml":[""],"/tmp/velero-4bf57ed2-velero/velero/templates/deployment.yaml":[""],"/tmp/velero-4bf57ed2-velero/velero/templates/restic-daemonset.yaml":[""],"/tmp/velero-4bf57ed2-velero/velero/templates/secret.yaml":[""]}
{"/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/deployment.yaml":[".spec.replicas: '2' != '0'"],"/tmp/autoscaler-fb12fa7a-cluster-autoscaler/cluster-autoscaler/templates/servicemonitor.yaml":["error: the server doesn't have a resource type \"ServiceMonitor\"\n"]}
{"/tmp/metrics-server-1960953a-metrics-server-certs/raw/templates/resources.yaml":["error: the server doesn't have a resource type \"Issuer\"\n","error: the server doesn't have a resource type \"Certificate\"\n"]}
$jq-s-r'map(@json)| join(“\n”)”

现在,您可以逐行处理它,而不必担心匹配花括号。

谢谢您的建议,上面的jq不适用于所有json负载。例如,对于下面的json负载,它给出了一个错误

 {
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-deployment.yaml": [
     ".spec.replicas: '2' != '3'",
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-metrics-service.yaml": [
     ".spec.clusterIP: '' != '10.3.24.53'"
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-service.yaml": [
     ".spec.clusterIP: '' != '10.3.115.118'"
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-stats-service.yaml": [
     ".spec.clusterIP: '' != '10.3.115.30'"
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/default-backend-deployment.yaml": [
   ]
 }

感谢您的建议,上面的jq不能适用于所有json负载。例如,对于下面的json负载,它给出了一个错误

 {
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-deployment.yaml": [
     ".spec.replicas: '2' != '3'",
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-metrics-service.yaml": [
     ".spec.clusterIP: '' != '10.3.24.53'"
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-service.yaml": [
     ".spec.clusterIP: '' != '10.3.115.118'"
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/controller-stats-service.yaml": [
     ".spec.clusterIP: '' != '10.3.115.30'"
   ],
   "/tmp/ingress-dae7bd30-ingress-internet/nginx-ingress/templates/default-backend-deployment.yaml": [
   ]
 }

请考虑反馈给具体答案作为对该答案的评论,而不是把它作为一个不同的答案张贴。请考虑提供反馈给具体答案作为评论的答案,而不是张贴它作为一个不同的答案。考虑发布确切的输出从给定的输入,为了避免误解所需要的内容,考虑从给定的输入中精确地输出,以避免误解所需要的内容。