awk命令結合管道命令對json文件進行統計分析

json文件內容:json

$ head file.json 
{"B": 0.337, "C": 0.663, "name": "xxx"}
{"B": 0.671, "C": 0.329, "name": "xxxxx"}
{"B": 0.445, "C": 0.555, "name": "xxxxxxx"}

 

要統計"B"的機率在(0.6,0.7]區間的數目,完整命令以下:spa

$ awk '{print $2}' file.json | awk -F ',' '{if($1 > 0.6 && $1 <= 0.7) {print $1}}' | wc -l

 

將"B"機率大於等於0.7的輸出:code

$ head file.json 
{"B": 0.671, "C": 0.329, "name": "xxx"}
{"B": 0.817, "C": 0.183, "name": "xxx"}
{"B": 0.719, "C": 0.281, "name": "xxx"}
{"B": 0.697, "C": 0.303, "name": "xxx"}
{"B": 0.674, "C": 0.326, "name": "xxx"}
{"B": 0.615, "C": 0.385, "name": "xxx"}
{"B": 0.732, "C": 0.268, "name": "xxx"}
{"B": 0.582, "C": 0.418, "name": "xxx"}
{"B": 0.563, "C": 0.437, "name": "xxx"}
{"B": 0.262, "C": 0.738, "name": "xxx"}

$ head file.json | awk '{if(substr($2, 0, 6) > 0.7) print $0}'  # 輸出整句
{"toB": 0.817, "toC": 0.183, "name": "xxx"}
{"toB": 0.719, "toC": 0.281, "name": "xxx"}
{"toB": 0.732, "toC": 0.268, "name": "xxx"}

$ head file.json | awk '{if(substr($2, 0, 6) >= 0.7) print $1, $2, $5, $6}'  # 輸出指定部分
{"B": 0.817, "name": "xxx"}
{"B": 0.732, "name": "xxx"}
{"B": 0.719, "name": "xxx"}

 

1.第一個awk沒有指定分隔符,默認使用空格進行分割blog

$ head file.json | awk '{print $2}'
0.337,
0.671,
0.445,

2.第二個awk再指定逗號做爲分隔符class

$ head file.json | awk '{print $2}' | awk -F ',' '{print $1}'
0.337
0.671
0.445
相關文章
相關標籤/搜索