免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3112 | 回复: 7
打印 上一主题 下一主题

[文本处理] shell处理json文件 [复制链接]

论坛徽章:
1
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2019-08-19 22:58 |只看该作者 |倒序浏览
cu的朋友们请帮忙,

json文件我是用curl每分钟在我们spring boot 微服务上去取。
需要执行效率高些,因为打算往日志服务器上传(logstash->es)。

json文件是嵌套了多个数组,数组数量不是固定的,是动态变化的。以下面这个json文件为例子包含了四个数组 “msgprocessors”:[],"endpoints":[],"services":[],"apis":[].
我的要求是如何将这个json文件里每个数组的内容取出来,放在json的{}里,每一个数组占一行,有多少数组就有多少行。



json 源文件
  1. {
  2.     "msgprocessors": [
  3.       {
  4.         "processor": "eshop-retryOrder-fail-processor",
  5.         "active": true
  6.       },
  7.       {
  8.         "processor": "eshop-order-processor",
  9.         "active": true
  10.       }
  11.     ],
  12.     "endpoints": [
  13.       {
  14.         "endpoint": "activeprocessor-admin-serviceEP",
  15.         "active": true
  16.       },
  17.       {
  18.         "endpoint": "api-admin-serviceEP",
  19.         "active": true
  20.       }
  21.     ],
  22.     "services": [
  23.       {
  24.         "service": "pt-filedecrypt-service",
  25.         "active": true
  26.       },
  27.       {
  28.         "service": "uk-file-processor-service",
  29.         "active": true
  30.       }
  31.     ],
  32.     "apis": [
  33.       {
  34.         "api": "appointmentLateness",
  35.         "active": true
  36.       },
  37.       {
  38.         "api": "bioqueue",
  39.         "active": true
  40.       },
  41.       {
  42.         "api": "pt-emetrics_TLS2PortugalAPI",
  43.         "active": true
  44.       }
  45.     ]
  46.   }
复制代码

需要输出样例文件如下(json数组数量不固定的,当前例子就是四个数组):
  1. 第一行:
  2. {
  3.     "msgprocessors": [
  4.       {
  5.         "processor": "eshop-retryOrder-fail-processor",
  6.         "active": true
  7.       },
  8.       {
  9.         "processor": "eshop-order-processor",
  10.         "active": true
  11.       }
  12.     ]
  13. }


  14. 第二行:

  15. {

  16. "endpoints": [
  17. {
  18. "endpoint": "activeprocessor-admin-serviceEP",
  19. "active": true
  20. },
  21. {
  22. "endpoint": "api-admin-serviceEP",
  23. "active": true
  24. }
  25. ]

  26. }

  27. 第三行:
  28. {
  29.     "services": [
  30.       {
  31.         "service": "pt-filedecrypt-service",
  32.         "active": true
  33.       },
  34.       {
  35.         "service": "uk-file-processor-service",
  36.         "active": true
  37.       }
  38.     ]
  39. }


  40. 第四行:

  41. {
  42. "apis": [
  43.       {
  44.         "api": "appointmentLateness",
  45.         "active": true
  46.       },
  47.       {
  48.         "api": "bioqueue",
  49.         "active": true
  50.       },
  51.       {
  52.         "api": "pt-emetrics_TLS2PortugalAPI",
  53.         "active": true
  54.       }
  55.     ]
  56. }
复制代码









论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
2 [报告]
发表于 2019-08-20 08:26 |只看该作者
试过jq命令吗?

论坛徽章:
8
2016科比退役纪念章
日期:2018-10-24 08:24:0115-16赛季CBA联赛之北控
日期:2019-03-12 14:34:562016科比退役纪念章
日期:2019-04-01 10:33:0915-16赛季CBA联赛之山东
日期:2019-04-17 12:46:3215-16赛季CBA联赛之广夏
日期:2019-05-09 16:40:4015-16赛季CBA联赛之广夏
日期:2019-10-10 15:33:4015-16赛季CBA联赛之辽宁
日期:2019-10-15 08:37:0615-16赛季CBA联赛之北控
日期:2021-03-30 15:53:34
3 [报告]
发表于 2019-08-20 15:10 |只看该作者
本帖最后由 cfwyy 于 2019-08-21 09:01 编辑

感觉正规路子应该用jq,
不过jq太强了,现学还没怎么会用
先来个野路子 sed

  1. $ sed -E 's/(\]),/\1\n\}\n\{/' json.txt
复制代码


用jq的话,jq本身的循环遍历没怎么搞懂,不知道能不能实现。
先用shell的循环来调jq,就是效率不高。

  1. #!/bin/bash

  2. jsonFile="json.txt"
  3. for i in `jq 'keys[]' $jsonFile`    #先取得属性名字
  4. do
  5.     k=`echo $i|tr -d '"'`           #去掉引号,否则会带引号传给jq
  6.     jq --arg t $k '{($t):.[$t]}' $jsonFile      # 按每个属性名:对应属性值 单独构造成json对象。
  7. done
复制代码

论坛徽章:
1
4 [报告]
发表于 2019-08-21 15:14 |只看该作者
本帖最后由 guobaofu 于 2019-08-21 15:20 编辑

回复 3# cfwyy

hi,我测试过你的sed命令和脚本,对于我上面的源文件输出可以,但是我的json文件内容比那个多,我测试输出有问题。


json.txt


  1. {
  2.   "Components": {
  3.     "msgprocessors": [
  4.       {
  5.         "processor": "eshop-retryOrder-fail-processor",
  6.         "active": true
  7.       },
  8.       {
  9.         "processor": "eshop-order-processor",
  10.         "active": true
  11.       },
  12.       {
  13.         "processor": "ch-retry-outbound-sampling",
  14.         "active": true
  15.       },
  16.       {
  17.         "processor": "pt-retry-outbound-sampling",
  18.         "active": true
  19.       },
  20.       {
  21.         "processor": "be-biometrics-inbox-processor",
  22.         "active": true
  23.       },
  24.       {
  25.         "processor": "pt-biometrics-inbox-processor",
  26.         "active": true
  27.       },
  28.       {
  29.         "processor": "ch-biometrics-inbox-processor",
  30.         "active": true
  31.       },
  32.       {
  33.         "processor": "uk-biometrics-inbox-processor",
  34.         "active": true
  35.       },
  36.       {
  37.         "processor": "eshop-retryAppointment-fail-processor",
  38.         "active": true
  39.       },
  40.       {
  41.         "processor": "be-retry-outbound-sampling",
  42.         "active": true
  43.       },
  44.       {
  45.         "processor": "uk-retry-outbound-sampling",
  46.         "active": true
  47.       },
  48.       {
  49.         "processor": "eshop-appointment-processor",
  50.         "active": true
  51.       },
  52.       {
  53.         "processor": "uk-priority-retry-outbound-sampling",
  54.         "active": true
  55.       }
  56.     ],
  57.     "endpoints": [
  58.       {
  59.         "endpoint": "activeprocessor-admin-serviceEP",
  60.         "active": true
  61.       },
  62.       {
  63.         "endpoint": "api-admin-serviceEP",
  64.         "active": true
  65.       },
  66.       {
  67.         "endpoint": "be-filedecrypt-service-endpoint",
  68.         "active": true
  69.       },
  70.       {
  71.         "endpoint": "ch-filedecrypt-service-endpoint",
  72.         "active": true
  73.       },
  74.       {
  75.         "endpoint": "drools_antifraud",
  76.         "active": true
  77.       },
  78.       {
  79.         "endpoint": "drools_appointment_lateness",
  80.         "active": true
  81.       },
  82.       {
  83.         "endpoint": "drools_bioqueue",
  84.         "active": true
  85.       },
  86.       {
  87.         "endpoint": "drools_dynamic_endpoint",
  88.         "active": true
  89.       },
  90.       {
  91.         "endpoint": "endpoint-admin-serviceEP",
  92.         "active": true
  93.       },
  94.       {
  95.         "endpoint": "eshop-out-appointment-endpoint",
  96.         "active": true
  97.       },
  98.       {
  99.         "endpoint": "eshop-out-order-endpoint",
  100.         "active": true
  101.       },
  102.       {
  103.         "endpoint": "eshop_token_endpoint",
  104.         "active": true
  105.       },
  106.       {
  107.         "endpoint": "lb-be-encrypt-endpoint",
  108.         "active": true
  109.       },
  110.       {
  111.         "endpoint": "msgprocessor-admin-serviceEP",
  112.         "active": true
  113.       },
  114.       {
  115.         "endpoint": "pt-filedecrypt-service-endpoint",
  116.         "active": true
  117.       },
  118.       {
  119.         "endpoint": "queue-admin-serviceEP",
  120.         "active": true
  121.       },
  122.       {
  123.         "endpoint": "servicegroup-admin-serviceEP",
  124.         "active": true
  125.       },
  126.       {
  127.         "endpoint": "tlsconnect_appointment_endpoint",
  128.         "active": true
  129.       },
  130.       {
  131.         "endpoint": "tlsconnect_order_endpoint",
  132.         "active": true
  133.       },
  134.       {
  135.         "endpoint": "tlsconnect_tma",
  136.         "active": true
  137.       },
  138.       {
  139.         "endpoint": "uk-filedecrypt-service-endpoint",
  140.         "active": true
  141.       },
  142.       {
  143.         "endpoint": "uk-hone1adapter-outbound-endpoint",
  144.         "active": true
  145.       }
  146.     ],
  147.     "services": [
  148.       {
  149.         "service": "pt-filedecrypt-service",
  150.         "active": true
  151.       },
  152.       {
  153.         "service": "uk-file-processor-service",
  154.         "active": true
  155.       },
  156.       {
  157.         "service": "uk-filedecrypt-service",
  158.         "active": true
  159.       },
  160.       {
  161.         "service": "uk-hone1adapter-outbound-service",
  162.         "active": true
  163.       },
  164.       {
  165.         "service": "Version",
  166.         "active": true
  167.       }
  168.     ],
  169.     "apis": [
  170.       {
  171.         "api": "appointmentLateness",
  172.         "active": true
  173.       },
  174.       {
  175.         "api": "bioqueue",
  176.         "active": true
  177.       },
  178.       {
  179.         "api": "pt-emetrics_TLS2PortugalAPI",
  180.         "active": true
  181.       },
  182.       {
  183.         "api": "order",
  184.         "active": true
  185.       },
  186.       {
  187.         "api": "be_token",
  188.         "active": true
  189.       },
  190.       {
  191.         "api": "GenericRulesEngineAPI",
  192.         "active": true
  193.       },
  194.       {
  195.         "api": "tma",
  196.         "active": true
  197.       },
  198.       {
  199.         "api": "sre",
  200.         "active": true
  201.       },
  202.       {
  203.         "api": "esbHealthCheckAPI",
  204.         "active": true
  205.       }
  206.     ]
  207.   }
  208. }
复制代码


cat  json.txt|sed -E 's/(\]),/\1\n\}\n\{/'

输出结果:

  1. {
  2.   "Components": {
  3.     "msgprocessors": [
  4.       {
  5.         "processor": "eshop-retryOrder-fail-processor",
  6.         "active": true
  7.       },
  8.       {
  9.         "processor": "eshop-order-processor",
  10.         "active": true
  11.       },
  12.       {
  13.         "processor": "ch-retry-outbound-sampling",
  14.         "active": true
  15.       },
  16.       {
  17.         "processor": "pt-retry-outbound-sampling",
  18.         "active": true
  19.       },
  20.       {
  21.         "processor": "be-biometrics-inbox-processor",
  22.         "active": true
  23.       },
  24.       {
  25.         "processor": "pt-biometrics-inbox-processor",
  26.         "active": true
  27.       },
  28.       {
  29.         "processor": "ch-biometrics-inbox-processor",
  30.         "active": true
  31.       },
  32.       {
  33.         "processor": "uk-biometrics-inbox-processor",
  34.         "active": true
  35.       },
  36.       {
  37.         "processor": "eshop-retryAppointment-fail-processor",
  38.         "active": true
  39.       },
  40.       {
  41.         "processor": "be-retry-outbound-sampling",
  42.         "active": true
  43.       },
  44.       {
  45.         "processor": "uk-retry-outbound-sampling",
  46.         "active": true
  47.       },
  48.       {
  49.         "processor": "eshop-appointment-processor",
  50.         "active": true
  51.       },
  52.       {
  53.         "processor": "uk-priority-retry-outbound-sampling",
  54.         "active": true
  55.       }
  56.     ]
  57. }
  58. {
  59.     "endpoints": [
  60.       {
  61.         "endpoint": "activeprocessor-admin-serviceEP",
  62.         "active": true
  63.       },
  64.       {
  65.         "endpoint": "api-admin-serviceEP",
  66.         "active": true
  67.       },
  68.       {
  69.         "endpoint": "be-filedecrypt-service-endpoint",
  70.         "active": true
  71.       },
  72.       {
  73.         "endpoint": "ch-filedecrypt-service-endpoint",
  74.         "active": true
  75.       },
  76.       {
  77.         "endpoint": "drools_antifraud",
  78.         "active": true
  79.       },
  80.       {
  81.         "endpoint": "drools_appointment_lateness",
  82.         "active": true
  83.       },
  84.       {
  85.         "endpoint": "drools_bioqueue",
  86.         "active": true
  87.       },
  88.       {
  89.         "endpoint": "drools_dynamic_endpoint",
  90.         "active": true
  91.       },
  92.       {
  93.         "endpoint": "endpoint-admin-serviceEP",
  94.         "active": true
  95.       },
  96.       {
  97.         "endpoint": "eshop-out-appointment-endpoint",
  98.         "active": true
  99.       },
  100.       {
  101.         "endpoint": "eshop-out-order-endpoint",
  102.         "active": true
  103.       },
  104.       {
  105.         "endpoint": "eshop_token_endpoint",
  106.         "active": true
  107.       },
  108.       {
  109.         "endpoint": "lb-be-encrypt-endpoint",
  110.         "active": true
  111.       },
  112.       {
  113.         "endpoint": "msgprocessor-admin-serviceEP",
  114.         "active": true
  115.       },
  116.       {
  117.         "endpoint": "pt-filedecrypt-service-endpoint",
  118.         "active": true
  119.       },
  120.       {
  121.         "endpoint": "queue-admin-serviceEP",
  122.         "active": true
  123.       },
  124.       {
  125.         "endpoint": "servicegroup-admin-serviceEP",
  126.         "active": true
  127.       },
  128.       {
  129.         "endpoint": "tlsconnect_appointment_endpoint",
  130.         "active": true
  131.       },
  132.       {
  133.         "endpoint": "tlsconnect_order_endpoint",
  134.         "active": true
  135.       },
  136.       {
  137.         "endpoint": "tlsconnect_tma",
  138.         "active": true
  139.       },
  140.       {
  141.         "endpoint": "uk-filedecrypt-service-endpoint",
  142.         "active": true
  143.       },
  144.       {
  145.         "endpoint": "uk-hone1adapter-outbound-endpoint",
  146.         "active": true
  147.       }
  148.     ]
  149. }
  150. {
  151.     "services": [
  152.       {
  153.         "service": "pt-filedecrypt-service",
  154.         "active": true
  155.       },
  156.       {
  157.         "service": "uk-file-processor-service",
  158.         "active": true
  159.       },
  160.       {
  161.         "service": "uk-filedecrypt-service",
  162.         "active": true
  163.       },
  164.       {
  165.         "service": "uk-hone1adapter-outbound-service",
  166.         "active": true
  167.       },
  168.       {
  169.         "service": "Version",
  170.         "active": true
  171.       }
  172.     ]
  173. }
  174. {
  175.     "apis": [
  176.       {
  177.         "api": "appointmentLateness",
  178.         "active": true
  179.       },
  180.       {
  181.         "api": "bioqueue",
  182.         "active": true
  183.       },
  184.       {
  185.         "api": "pt-emetrics_TLS2PortugalAPI",
  186.         "active": true
  187.       },
  188.       {
  189.         "api": "order",
  190.         "active": true
  191.       },
  192.       {
  193.         "api": "be_token",
  194.         "active": true
  195.       },
  196.       {
  197.         "api": "GenericRulesEngineAPI",
  198.         "active": true
  199.       },
  200.       {
  201.         "api": "tma",
  202.         "active": true
  203.       },
  204.       {
  205.         "api": "sre",
  206.         "active": true
  207.       },
  208.       {
  209.         "api": "esbHealthCheckAPI",
  210.         "active": true
  211.       }
  212.     ]
  213.   }
  214. }
复制代码

jq输出有问题啊cat json-out.txt |jq -c                                             
parse error: Expected separator between values at line 58, column 1   




希望输出结果是这样子:

  1. {
  2.     "msgprocessors": [
  3.       {
  4.         "processor": "eshop-retryOrder-fail-processor",
  5.         "active": true
  6.       },
  7.       {
  8.         "processor": "eshop-order-processor",
  9.         "active": true
  10.       },
  11.       {
  12.         "processor": "ch-retry-outbound-sampling",
  13.         "active": true
  14.       },
  15.       {
  16.         "processor": "pt-retry-outbound-sampling",
  17.         "active": true
  18.       },
  19.       {
  20.         "processor": "be-biometrics-inbox-processor",
  21.         "active": true
  22.       },
  23.       {
  24.         "processor": "pt-biometrics-inbox-processor",
  25.         "active": true
  26.       },
  27.       {
  28.         "processor": "ch-biometrics-inbox-processor",
  29.         "active": true
  30.       },
  31.       {
  32.         "processor": "uk-biometrics-inbox-processor",
  33.         "active": true
  34.       },
  35.       {
  36.         "processor": "eshop-retryAppointment-fail-processor",
  37.         "active": true
  38.       },
  39.       {
  40.         "processor": "be-retry-outbound-sampling",
  41.         "active": true
  42.       },
  43.       {
  44.         "processor": "uk-retry-outbound-sampling",
  45.         "active": true
  46.       },
  47.       {
  48.         "processor": "eshop-appointment-processor",
  49.         "active": true
  50.       },
  51.       {
  52.         "processor": "uk-priority-retry-outbound-sampling",
  53.         "active": true
  54.       }
  55.     ]
  56. }
  57. {
  58.     "endpoints": [
  59.       {
  60.         "endpoint": "activeprocessor-admin-serviceEP",
  61.         "active": true
  62.       },
  63.       {
  64.         "endpoint": "api-admin-serviceEP",
  65.         "active": true
  66.       },
  67.       {
  68.         "endpoint": "be-filedecrypt-service-endpoint",
  69.         "active": true
  70.       },
  71.       {
  72.         "endpoint": "ch-filedecrypt-service-endpoint",
  73.         "active": true
  74.       },
  75.       {
  76.         "endpoint": "drools_antifraud",
  77.         "active": true
  78.       },
  79.       {
  80.         "endpoint": "drools_appointment_lateness",
  81.         "active": true
  82.       },
  83.       {
  84.         "endpoint": "drools_bioqueue",
  85.         "active": true
  86.       },
  87.       {
  88.         "endpoint": "drools_dynamic_endpoint",
  89.         "active": true
  90.       },
  91.       {
  92.         "endpoint": "endpoint-admin-serviceEP",
  93.         "active": true
  94.       },
  95.       {
  96.         "endpoint": "eshop-out-appointment-endpoint",
  97.         "active": true
  98.       },
  99.       {
  100.         "endpoint": "eshop-out-order-endpoint",
  101.         "active": true
  102.       },
  103.       {
  104.         "endpoint": "eshop_token_endpoint",
  105.         "active": true
  106.       },
  107.       {
  108.         "endpoint": "lb-be-encrypt-endpoint",
  109.         "active": true
  110.       },
  111.       {
  112.         "endpoint": "msgprocessor-admin-serviceEP",
  113.         "active": true
  114.       },
  115.       {
  116.         "endpoint": "pt-filedecrypt-service-endpoint",
  117.         "active": true
  118.       },
  119.       {
  120.         "endpoint": "queue-admin-serviceEP",
  121.         "active": true
  122.       },
  123.       {
  124.         "endpoint": "servicegroup-admin-serviceEP",
  125.         "active": true
  126.       },
  127.       {
  128.         "endpoint": "tlsconnect_appointment_endpoint",
  129.         "active": true
  130.       },
  131.       {
  132.         "endpoint": "tlsconnect_order_endpoint",
  133.         "active": true
  134.       },
  135.       {
  136.         "endpoint": "tlsconnect_tma",
  137.         "active": true
  138.       },
  139.       {
  140.         "endpoint": "uk-filedecrypt-service-endpoint",
  141.         "active": true
  142.       },
  143.       {
  144.         "endpoint": "uk-hone1adapter-outbound-endpoint",
  145.         "active": true
  146.       }
  147.     ]
  148. }
  149. {
  150.     "services": [
  151.       {
  152.         "service": "pt-filedecrypt-service",
  153.         "active": true
  154.       },
  155.       {
  156.         "service": "uk-file-processor-service",
  157.         "active": true
  158.       },
  159.       {
  160.         "service": "uk-filedecrypt-service",
  161.         "active": true
  162.       },
  163.       {
  164.         "service": "uk-hone1adapter-outbound-service",
  165.         "active": true
  166.       },
  167.       {
  168.         "service": "Version",
  169.         "active": true
  170.       }
  171.     ]
  172. }
  173. {
  174.     "apis": [
  175.       {
  176.         "api": "appointmentLateness",
  177.         "active": true
  178.       },
  179.       {
  180.         "api": "bioqueue",
  181.         "active": true
  182.       },
  183.       {
  184.         "api": "pt-emetrics_TLS2PortugalAPI",
  185.         "active": true
  186.       },
  187.       {
  188.         "api": "order",
  189.         "active": true
  190.       },
  191.       {
  192.         "api": "be_token",
  193.         "active": true
  194.       },
  195.       {
  196.         "api": "GenericRulesEngineAPI",
  197.         "active": true
  198.       },
  199.       {
  200.         "api": "tma",
  201.         "active": true
  202.       },
  203.       {
  204.         "api": "sre",
  205.         "active": true
  206.       },
  207.       {
  208.         "api": "esbHealthCheckAPI",
  209.         "active": true
  210.       }
  211.     ]
  212.   }
复制代码


cat json-out.txt |jq -c

  1. {"msgprocessors":[{"processor":"eshop-retryOrder-fail-processor","active":true},{"processor":"eshop-order-processor","active":true},{"processor":"ch-retry-outbound-sampling","active":true},{"processor":"pt-retry-outbound-sampling","active":true},{"processor":"be-biometrics-inbox-processor","active":true},{"processor":"pt-biometrics-inbox-processor","active":true},{"processor":"ch-biometrics-inbox-processor","active":true},{"processor":"uk-biometrics-inbox-processor","active":true},{"processor":"eshop-retryAppointment-fail-processor","active":true},{"processor":"be-retry-outbound-sampling","active":true},{"processor":"uk-retry-outbound-sampling","active":true},{"processor":"eshop-appointment-processor","active":true},{"processor":"uk-priority-retry-outbound-sampling","active":true}]}
  2. {"endpoints":[{"endpoint":"activeprocessor-admin-serviceEP","active":true},{"endpoint":"api-admin-serviceEP","active":true},{"endpoint":"be-filedecrypt-service-endpoint","active":true},{"endpoint":"ch-filedecrypt-service-endpoint","active":true},{"endpoint":"drools_antifraud","active":true},{"endpoint":"drools_appointment_lateness","active":true},{"endpoint":"drools_bioqueue","active":true},{"endpoint":"drools_dynamic_endpoint","active":true},{"endpoint":"endpoint-admin-serviceEP","active":true},{"endpoint":"eshop-out-appointment-endpoint","active":true},{"endpoint":"eshop-out-order-endpoint","active":true},{"endpoint":"eshop_token_endpoint","active":true},{"endpoint":"lb-be-encrypt-endpoint","active":true},{"endpoint":"msgprocessor-admin-serviceEP","active":true},{"endpoint":"pt-filedecrypt-service-endpoint","active":true},{"endpoint":"queue-admin-serviceEP","active":true},{"endpoint":"servicegroup-admin-serviceEP","active":true},{"endpoint":"tlsconnect_appointment_endpoint","active":true},{"endpoint":"tlsconnect_order_endpoint","active":true},{"endpoint":"tlsconnect_tma","active":true},{"endpoint":"uk-filedecrypt-service-endpoint","active":true},{"endpoint":"uk-hone1adapter-outbound-endpoint","active":true}]}
  3. {"services":[{"service":"pt-filedecrypt-service","active":true},{"service":"uk-file-processor-service","active":true},{"service":"uk-filedecrypt-service","active":true},{"service":"uk-hone1adapter-outbound-service","active":true},{"service":"Version","active":true}]}
  4. {"apis":[{"api":"appointmentLateness","active":true},{"api":"bioqueue","active":true},{"api":"pt-emetrics_TLS2PortugalAPI","active":true},{"api":"order","active":true},{"api":"be_token","active":true},{"api":"GenericRulesEngineAPI","active":true},{"api":"tma","active":true},{"api":"sre","active":true},{"api":"esbHealthCheckAPI","active":true}]}
复制代码





论坛徽章:
8
2016科比退役纪念章
日期:2018-10-24 08:24:0115-16赛季CBA联赛之北控
日期:2019-03-12 14:34:562016科比退役纪念章
日期:2019-04-01 10:33:0915-16赛季CBA联赛之山东
日期:2019-04-17 12:46:3215-16赛季CBA联赛之广夏
日期:2019-05-09 16:40:4015-16赛季CBA联赛之广夏
日期:2019-10-10 15:33:4015-16赛季CBA联赛之辽宁
日期:2019-10-15 08:37:0615-16赛季CBA联赛之北控
日期:2021-03-30 15:53:34
5 [报告]
发表于 2019-08-21 16:14 |只看该作者
回复 4# guobaofu

你现在的例子 和你一开始给出的例子 不是内容多和少的问题,是结构不一样,当然直接用肯定有问题了。
我sed的想法就是刚好按你一开始的结构,简单的做两个括号的替换就可以了。
可以联合jq稍改一下。
  1. jq '.[]' 2.txt |sed -E 's/(\]),/\1\n\}\n\{/'
复制代码


脚本的方法根据新的json结构也可以改一下:
  1. #!/bin/bash

  2. jsonFile="json.txt"
  3. for i in `jq '.[]|keys[]' $jsonFile`
  4. do
  5.     k=`echo $i|tr -d '"'`
  6.     jq --arg t $k '{($t):.[][$t]}' $jsonFile
  7. done
复制代码


论坛徽章:
0
6 [报告]
发表于 2019-08-27 14:31 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
7 [报告]
发表于 2019-08-29 13:32 |只看该作者
回复 6# 本友会机友会摄友会

多谢兄台相助,好用了。

论坛徽章:
1
8 [报告]
发表于 2019-08-29 13:33 |只看该作者
回复 5# cfwyy

仁兄,你的我已经测试了,妥妥的好用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP