BASH:将多行(JSON)块转换为单行字符串
我将下面的JSON输出写入“out”文件,内容如下 $cat outBASH:将多行(JSON)块转换为单行字符串,json,bash,Json,Bash,我将下面的JSON输出写入“out”文件,内容如下 $cat out { "columns": [ "Tests", "Errors", "Mean Test Time (ms)", "Test Time Standard Deviation (ms)", "TPS", "Peak TPS" ], "status": { "description": "Coll
{
"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
]
}
我需要与描述块“reads 95%”相关的统计数据以下面的格式排列,并使用BASH脚本分配给字符串变量
var=304000,0,8.7931875,7.948696618436826,6907.677974959667,13594.0
非常感谢您的帮助。您需要为此使用专用的命令行JSON解析器,例如: 安装下划线后,您可以执行以下操作:
cat data.json | underscore select '.description, .statistics first-child'| tr -d '[]'
编辑:Sed-only解决方案(小心使用):
为此,您需要使用专用的命令行JSON解析器,例如: 安装下划线后,您可以执行以下操作:
cat data.json | underscore select '.description, .statistics first-child'| tr -d '[]'
编辑:Sed-only解决方案(小心使用):
下面是我的“纯壳”解决方案
cat data.json | sed 's/^[ \t]*//;s/[ \t]*$//' | awk -F ': *' 'BEGIN { RS=",\n\"|\n}," } { gsub(/[\n\]\[\}]/,"",$2); if ($2) { printf("%s,", $2); } }'
下面是我的“纯壳”解决方案
cat data.json | sed 's/^[ \t]*//;s/[ \t]*$//' | awk -F ': *' 'BEGIN { RS=",\n\"|\n}," } { gsub(/[\n\]\[\}]/,"",$2); if ($2) { printf("%s,", $2); } }'
out文件包含Grinder(一种开源负载测试工具)测试数据,这些数据被解析为JSON
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
]
}
给出以下输出-测试“读取95%”的测试统计信息
下一个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'
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
},
注意:我不是这个主题领域的专家,因此这可能不是最有效的解决方案输出文件包含解析为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
]
}
给出以下输出-测试“读取95%”的测试统计信息
下一个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'
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
},
sudo pip install sql4json
命令
cat out|sql4json --csv 'SELECT statistics FROM tests WHERE description=="Reads 95%"'
输出到标准输出:
304000,0,8.7931875,7.948696618446907.6779749613594.0
试试sql4json()
安装:
sudo pip install sql4json
命令
cat out|sql4json --csv 'SELECT statistics FROM tests WHERE description=="Reads 95%"'
输出到标准输出:
304000,0,8.7931875,7.948696618446907.6779749613594.0
谢谢你的支持。顺便说一句,我可以使用bash将输出安排如下。304000,0,8.7931875,7.9486966184368266907.67797495966713594.0使用下划线您可以简单地完成:
cat data.json |下划线提取“统计信息”
也可以使用纯bash工具完成,但让我告诉您,它很快就会破坏它。更准确地说,我在操作了以下两个步骤后得到了这个块。步骤1:curl-s-xget | python-mjson.tool>退出步骤2:sed-nr'H/读取95%/,/\}/{s/(\})/\1/;Tx;p} )/\{/{x;s/*\n.*/;x;H}out注意:out包含许多类似于我在问题中给出的块的块。据我所知,您的解决方案听起来像是一个json文件。感谢您的指导和支持感谢您宝贵的支持。我从下面得到了预期的结果。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'| sed-e's/^[\t]*/'| tr“\\n”“| sed-e's/[\t]//g、 /^$/d’感谢您的支持。顺便说一句,我可以使用bash.304000,0,8.7931875,7.9486966184368266907.67797495966713594.0将输出安排如下:使用下划线您可以简单地执行:cat data.json |下划线提取“统计信息”
因此,也可以使用纯bash工具完成,但我告诉您,它很快就会崩溃更准确地说,我在操作了以下两个步骤后得到了这个块。步骤1:curl-s-X GET | python-mjson.tool>out步骤2:sed-nr'H;/Reads 95%/,/\}/{s/(\})/\1/;Tx;p} )/\{/{x;s/*\n.*/;x;H}out注意:out包含许多类似于我在问题中给出的块的块。据我所知,您的解决方案听起来像是一个json文件。感谢您的指导和支持感谢您宝贵的支持。我从下面得到了预期的结果。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'| sed-e's/^[\t]*/'| tr“\\n”“| sed-e's/[\t]//g、 /^$/d'非常感谢您的支持。我已经尝试过您的解决方案,它在一行中给出所有内容,并用逗号分隔。但是,这需要进一步操作,以从无iTunes使用的cat
spotted中提取所需部分!非常感谢您的支持。我已经尝试过您的解决方案,它在一行中给出所有内容,并用逗号d分隔Elimmited.然而,这需要进一步操作,以从无需使用cat
spoted的情况下提取预期部分!