Shell脚本将5个或更多json文件连接在一起

Shell脚本将5个或更多json文件连接在一起,第1张

概述我正在研究一个在大文件中有很多json文档的项目,我们可以将其称为manifest.json这些文件的标题如A-11.json{'id':'a-11', 'name':'XN0', 'code':'H3A8FF82820F' 'status':'live' } A-03.json{'id':'a-03', 'name':'PF1', 'co

我正在研究一个在大文件中有很多Json文档的项目,我们可以将其称为manifest.Json

这些文件的标题如

A-11.Json

{"ID":"a-11","name":"XN0","code":"H3A8FF82820F"  "status":"live"} 

A-03.Json

{"ID":"a-03","name":"PF1","code":"FFFF82820F"  "status":"live"}

A-09.Json

{"ID":"a-09","code":"FFFF82820F" "status":"live"} 

我想要一个shell脚本做的是以Alpha顺序连接它们,我还需要像这样包装它们:
    [
    {Json doc},
    {Json doc},
    {Json doc
    ]
用一个sq括号分隔a,使它看起来像下面的代码 –

join命令只连接两个文件,这样就无法工作了,我尝试了cat和ls的组合,但这一切都有点不对劲.我试图在这里使用linux环境而不是MS环境.

的manifest.Json

[{"ID":"a-03","code":"FFFF82820F"  "status":"live"},{"ID":"a-09",{"ID":"a-11","code":"H3A8FF82820F"  "status":"live"}]

命令

cat a-*.Json > manifest.Json

给我以下a-11.Json doc在顶部,任何帮助赞赏.

[{"ID":"a-11","code":"H3A8FF82820F"  "status":"live"}{"ID":"a-03",]
最佳答案使用以下bash脚本(它使用不是所有shell的标准数组):

#!/bin/bashshopt -s nullglobdeclare -a JsonsJsons=(a-*.Json) # ${Jsons[@]} Now contains the List of files to concatenateecho '[' > manifest.Jsonif [ ${#Jsons[@]} -gt 0 ]; then # if the List is not empty  cat "${Jsons[0]}" >> manifest.Json # concatenate the first file to the manifest...  unset Jsons[0]                     # and remove it from the List  for f in "${Jsons[@]}"; do         # iterate over the rest      echo "," >>manifest.Json      cat "$f" >>manifest.Json  donefiecho ']' >>manifest.Json             # complete the manifest
总结

以上是内存溢出为你收集整理的Shell脚本将5个或更多json文件连接在一起全部内容,希望文章能够帮你解决Shell脚本将5个或更多json文件连接在一起所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1048126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存