shell 如何在bash中将纽曼响应字符串转换为json响应[已关闭]

y53ybaqx  于 2023-02-05  发布在  Shell
关注(0)|答案(1)|浏览(107)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

3天前关闭。
Improve this question
我正在寻找转换一个表格式的字符串,这是一个纽曼响应JSON格式。下面是字符串

┌─────────────────────────┬────────────────────┬───────────────────┐
 │                         │           executed │            failed │
 ├─────────────────────────┼────────────────────┼───────────────────┤
 │              iterations │                  1 │                 0 │
 ├─────────────────────────┼────────────────────┼───────────────────┤
 │                requests │                 30 │                 0 │
 ├─────────────────────────┼────────────────────┼───────────────────┤
 │            test-scripts │                 60 │                 0 │
 ├─────────────────────────┼────────────────────┼───────────────────┤
 │      prerequest-scripts │                 38 │                 0 │
 ├─────────────────────────┼────────────────────┼───────────────────┤
 │              assertions │                 56 │                 0 │
 ├─────────────────────────┴────────────────────┴───────────────────┤
 │ total run duration: 8.9s                                         │
 ├──────────────────────────────────────────────────────────────────┤
 │ total data received: 8.19kB (approx)                             │
 ├──────────────────────────────────────────────────────────────────┤
 │ average response time: 267ms                                     │
 └──────────────────────────────────────────────────────────────────┘

我正在尝试将上述内容转换为如下所示的json响应

{
     "iterations": {
            "executed" : 1,
            "failed" : 0
            },
     "requests":{
            "executed" : 30,
            "failed" : 0
        },
     "test-scripts":{
            "executed" : 60,
            "failed" : 0
        },
    "prerequest-scripts":{
            "executed" : 38,
            "failed" : 0
        },
    "assertions":{
            "executed" : 60,
            "failed" : 0
        },
     "total run duration": "8.9s"    
     "total data received": "8.19kB (approx)"          
     "average response time": "267ms"  
}

有人能帮我一下吗?

syqv5f0l

syqv5f0l1#

给定 exact 字符串:

$ echo "$s"
┌─────────────────────────┬────────────────────┬───────────────────┐
│                         │           executed │            failed │
├─────────────────────────┼────────────────────┼───────────────────┤
│              iterations │                  1 │                 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│                requests │                 30 │                 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│            test-scripts │                 60 │                 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│      prerequest-scripts │                 38 │                 0 │
├─────────────────────────┼────────────────────┼───────────────────┤
│              assertions │                 56 │                 0 │
├─────────────────────────┴────────────────────┴───────────────────┤
│ total run duration: 8.9s                                         │
├──────────────────────────────────────────────────────────────────┤
│ total data received: 8.19kB (approx)                             │
├──────────────────────────────────────────────────────────────────┤
│ average response time: 267ms                                     │
└──────────────────────────────────────────────────────────────────┘

您可以使用这个ruby:

echo "$s" | ruby -r json -e 's=$<.read
d={}
s.scan(/^\W*([a-zA-Z\-]+)\W*(\d+)\W*(\d+)/).
    map{|k,e,f| {"#{k}":{"executed":e, "failed":f}} }.
    each{|h| d.merge!(h) }

s.scan(/^(?=.*:)[ \t]*│[ \t]*([^:]*):[ \t]*(.*?)(?:[ \t]{2,}|│)/).
    map{|k,v| {"#{k}":v}}.
    each{|h| d.merge!(h) }

puts JSON.pretty_generate(d)'

图纸:

{
  "iterations": {
    "executed": "1",
    "failed": "0"
  },
  "requests": {
    "executed": "30",
    "failed": "0"
  },
  "test-scripts": {
    "executed": "60",
    "failed": "0"
  },
  "prerequest-scripts": {
    "executed": "38",
    "failed": "0"
  },
  "assertions": {
    "executed": "56",
    "failed": "0"
  },
  "total run duration": "8.9s",
  "total data received": "8.19kB (approx)",
  "average response time": "267ms"
}

相关问题