linux 需要帮助将表格格式的字符串转换为JSON响应

dddzy1tm  于 2023-03-01  发布在  Linux
关注(0)|答案(2)|浏览(142)

我正在寻找帮助,在 shell 脚本转换为JSON响应下表格式化字符串。

┌─────────────────────────┬─────────────────────┬────────────────────┐
│                         │            executed │             failed │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              iterations │                   1 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│                requests │                  24 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│            test-scripts │                  48 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│      prerequest-scripts │                  28 │                  0 │
├─────────────────────────┼─────────────────────┼────────────────────┤
│              assertions │                  44 │                  0 │
├─────────────────────────┴─────────────────────┴────────────────────┤
│ total run duration: 8s                                             │
├────────────────────────────────────────────────────────────────────┤
│ total data received: 8.02kB (approx)                               │
├────────────────────────────────────────────────────────────────────┤
│
└───────────────────────────────────────────────────────────────────┘

作为

{
  "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"
}

我正在寻找一个像一些jq命令作为Ruby shell 脚本将不会在我的环境中工作。有人能帮助我吗
我已经尝试删除特殊字符并重新创建字符串,但无法达到预期的结果。

gmxoilav

gmxoilav1#

尝试使用AWK。从许多互联网示例中学习。此脚本可帮助您接近:

BEGIN           { h["│"]; h["total"]; h[""] }
/total run/     { labelC=$2 " " $3 " " $4; valueC=$5 }
/total data/    { labelD=$2 " " $3 " " $4; valueD=$5 " " $6 }
                { if ($2 in h) {} else { executed[$2]=$4; failed[$2]=$6 } }
END     { print "{"
          for (x in executed) { print "\"" x "\": {\"executed\": \"" executed[x] "\", \"failed\": \"" failed[x] "\"}," }
          print "\"" labelC "\": \"" valueC "\","
          print "\"" labelD "\": \"" valueD "\","
          print "\"average response time\": \"unknown\""
          print "}"
        }

将脚本保存为“format.awk”并对input.txt文件运行AWK,如下所示:

% awk -f format.awk input.txt
{
"iterations": {"executed": "1", "failed": "0"},
"requests": {"executed": "24", "failed": "0"},
"assertions": {"executed": "44", "failed": "0"},
"prerequest-scripts": {"executed": "28", "failed": "0"},
"test-scripts": {"executed": "48", "failed": "0"},
"total run duration:": "8s",
"total data received:": "8.02kB (approx)",
"average response time": "unknown"
}
ewm0tg9j

ewm0tg9j2#

我采用了不同的方法,也使用awk
文件脚本. awk

BEGIN { printf("{\n") }
/iterations/ || /requests/ || /test-scripts/ || /prerequest-scripts/ || /assertions/ {
    printf("  \"%s\": {\n", $2)
    printf("    \"executed\": \"%s\",\n", $4)
    printf("    \"failed\": \"%s\"\n", $6)
    printf("  },\n")
}
/total run duration/ {
    printf("  \"%s %s %s\" \"%s\",\n", $2, $3, $4, $5)
}
/total data received/ {
    printf("  \"%s %s %s\" \"%s %s\",\n", $2, $3, $4, $5, $6)
}
/average response time/ {
    printf("  \"%s %s %s\" \"%s\"\n", $2, $3, $4, $5)
}
END { printf("}\n") }

文件脚本. bash

#!/bin/bash

awk -f script.awk table.txt
  • 要执行,请运行./script.bash(或者像在bash中那样直接调用awk命令)。
  • table.txt包含您的表。
  • 对于"平均响应时间",我假设它在您的问题中的示例输入中缺失,所以我将其添加到我的表中。(平均响应时间:234ms)在样品中的空行中。
  • 如果"平均响应时间"是从上述数据计算出来的,你必须在你的问题中包括如何做到这一点。
  • 输出为:
{
  "iterations": {
    "executed": "1",
    "failed": "0"
  },
  "requests": {
    "executed": "24",
    "failed": "0"
  },
  "test-scripts": {
    "executed": "48",
    "failed": "0"
  },
  "prerequest-scripts": {
    "executed": "28",
    "failed": "0"
  },
  "assertions": {
    "executed": "44",
    "failed": "0"
  },
  "total run duration:" "8s",
  "total data received:" "8.02kB (approx)",
  "average response time:" "234ms"
}

相关问题