awk命令结合管道命令对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]区间的数目,完整命令如下:
$ awk '{print $2}' file.json | awk -F ',' '{if($1 > 0.6 && $1 <= 0.7) {print $1}}' | wc -l
将"B"概率大于等于0.7的输出:
$ 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没有指定分隔符,默认使用空格进行分割
$ head file.json | awk '{print $2}' 0.337, 0.671, 0.445,
2.第二个awk再指定逗号作为分隔符
$ head file.json | awk '{print $2}' | awk -F ',' '{print $1}' 0.337 0.671 0.445