2

我将以下 JSON 输出写入“out”文件,内容如下。

$猫出去

{
    "columns": [
        "Tests",
        "Errors",
        "Mean Test Time (ms)",
        "Test Time Standard Deviation (ms)",
        "TPS",
        "Peak TPS"
    ],
    "status": {
        "description": "Collection stopped",
        "state": "Stopped"
    },
    "tests": [
        {
            "description": "Cheetah client test",
            "statistics": [
                0,
                0,
                "NaN",
                0.0,
                0.0,
                0.0
            ],
            "test": 1
        },
        {
            "description": "Reads 95%",
            "statistics": [
                304000,
                0,
                8.7931875,
                7.948696618436826,
                6907.677974959667,
                13594.0
            ],
            "test": 101
        },
        {
            "description": "Writes 5%",
            "statistics": [
                16000,
                0,
                9.963375,
                9.92775949594747,
                363.5619986820878,
                1638.0
            ],
            "test": 102
        }
    ],
    "totals": [
        320000,
        0,
        8.851696875,
        8.063234652303947,
        7271.239973641756,
        14259.0
    ]
}

我需要与描述块“读取 95%”相关的统计信息以以下格式排列,并使用 BASH 脚本分配给字符串变量。

var=304000,0,8.7931875,7.948696618436826,6907.677974959667,13594.0

非常感激您的帮忙。

4

4 回答 4

1

您需要为此使用专用的命令行 JSON 解析器,例如下划线

安装下划线后,您可以执行以下操作:

cat data.json | underscore select '.description, .statistics first-child'| tr -d '[]'

编辑:仅 Sed 解决方案(谨慎使用):

sed -rn '/"description": *"Reads 95%",/,/],/{/statistics|description/!{1h; 1!H;};/],/{x;s/ *\n *|^ *|],//gp;};}' out

# gives 304000,0,8.7931875,7.948696618436826,6907.677974959667,13594.0
于 2013-06-24T10:46:48.200 回答
0

我的“纯壳”解决方案如下

cat data.json | sed 's/^[ \t]*//;s/[ \t]*$//' | awk  -F ': *' 'BEGIN { RS=",\n\"|\n}," } { gsub(/[\n\]\[\}]/,"",$2); if ($2) { printf("%s,", $2); } }'
于 2013-06-24T11:56:32.453 回答
0

out 文件包含解析为 JSON 的 Grinder(一个开源负载测试工具)测试数据

curl -s -X GET http://<localhost>:6373/recording/data' | python -mjson.tool > out

~$ cat out

{
    "columns": [
        "Tests",
        "Errors",
        "Mean Test Time (ms)",
        "Test Time Standard Deviation (ms)",
        "TPS",
        "Peak TPS"
    ],
    "status": {
        "description": "Collection stopped",
        "state": "Stopped"
    },
    "tests": [
        {
            "description": "Cheetah client test",
            "statistics": [
                0,
                0,
                "NaN",
                0.0,
                0.0,
                0.0
            ],
            "test": 1
        },
        {
            "description": "Reads 95%",
            "statistics": [
                304000,
                0,
                8.7931875,
                7.948696618436826,
                6907.677974959667,
                13594.0
            ],
            "test": 101
        },
        {
            "description": "Writes 5%",
            "statistics": [
                16000,
                0,
                9.963375,
                9.92775949594747,
                363.5619986820878,
                1638.0
            ],
            "test": 102
        }
    ],
    "totals": [
        320000,
        0,
        8.851696875,
        8.063234652303947,
        7271.239973641756,
        14259.0
    ]
}

sed 命令:

sed -nr "H;/$name/,/\}/{s/(\})/\1/;T;x;p};/\{/{x;s/.*\n.*//;x;H}" out | sed -e     '1,/statistics/d' | sed -e '/test/,$d' | sed '$d' | sed '/^$/d' | tr "\\n" " " | sed -e 's/[\t ]//g;/^$/d'

给出以下输出 - 测试“读取 95%”的测试统计数据

304000,0,8.7931875,7.948696618436826,6907.677974959667,13594.0

下一个 sed 命令:

sed -nr "H;/"Reads 95%"/,/\}/{s/(\})/\1/;T;x;p};/\{/{x;s/.*\n.*//;x;H}" out gives

    {
        "description": "Reads 95%",
        "statistics": [
            304000,
            0,
            8.7931875,
            7.948696618436826,
            6907.677974959667,
            13594.0
        ],
        "test": 101
    },
  1. 管道结果 1 与 | sed -e '1,/statistics/d' 给出包含“统计”的行之后的所有行

            304000,
            0,
            8.7931875,
            7.948696618436826,
            6907.677974959667,
            13594.0
        ],
        "test": 101
    },
    
  2. 管道结果 2 与 | sed -e '/test/,$d' 给出行以上的行包含“test”

            304000,
            0,
            8.7931875,
            7.948696618436826,
            6907.677974959667,
            13594.0
        ],
    
  3. 管道结果 3 与 | sed '$d' 删除输出 3 的最后一行 304000, 0, 8.7931875, 7.948696618436826, 6907.677974959667, 13594.0

  4. 管道结果 4 与 | tr "\n" " " | sed -e 's/[\t ]//g;/^$/d' 给出

    304000,0,8.7931875,7.948696618436826,6907.677974959667,13594.0

tr "\n" " " 用空格替换 CR(或 \n)并 sed -e 's/[\t ]//g;/^$/d' 删除所有空格和制表符

注意:我不是该主题领域的专家,因此这可能不是最有效的解决方案

于 2013-06-25T06:02:58.323 回答
0

试试 sql4json ( http://github.com/bheni/sql4json )

安装:

sudo pip install sql4json

命令

cat out|sql4json --csv 'SELECT statistics FROM tests WHERE description=="Reads 95%"'

输出到标准输出:

304000,0,8.7931875,7.94869661844,6907.67797496,13594.0

于 2013-12-16T17:29:48.747 回答