根据shell脚本中的第一个col计算第二列的平均值

时间:2016-01-11 10:00:40

标签: linux bash shell

我有一个像这样的输入日志文件:

CLASS:CLASS1, CREATE_TIME:30-Dec-2015 00:00:06, VALID_AFTER_TIME:30-Dec-2015 00:00:12, EVAL_TIME:30-Dec-2015 00:00:12, SUCCESS:true, TIME_TAKEN:1842
CLASS:CLASS1, CREATE_TIME:30-Dec-2015 00:00:06, VALID_AFTER_TIME:30-Dec-2015 00:00:12, EVAL_TIME:30-Dec-2015 00:00:12, SUCCESS:true, TIME_TAKEN:1842
CLASS:CLASS2, CREATE_TIME:30-Dec-2015 00:00:06, VALID_AFTER_TIME:30-Dec-2015 00:00:12, EVAL_TIME:30-Dec-2015 00:00:12, SUCCESS:true, TIME_TAKEN:1842
CLASS:CLASS1, CREATE_TIME:30-Dec-2015 00:00:06, VALID_AFTER_TIME:30-Dec-2015 00:00:12, EVAL_TIME:30-Dec-2015 00:00:12, SUCCESS:true, TIME_TAKEN:1842
CLASS:CLASS2, CREATE_TIME:30-Dec-2015 00:00:06, VALID_AFTER_TIME:30-Dec-2015 00:00:12, EVAL_TIME:30-Dec-2015 00:00:12, SUCCESS:true, TIME_TAKEN:1842
CLASS:CLASS1, CREATE_TIME:30-Dec-2015 00:00:06, VALID_AFTER_TIME:30-Dec-2015 00:00:12, EVAL_TIME:30-Dec-2015 00:00:12, SUCCESS:true, TIME_TAKEN:1842

我想计算每个班级的平均时间。 例如,我想要输出,如下所示:

CLASS1 5.5
CLASS2 13

我使用以下内容提取 名称时间,但无法计算平均值。

awk -F'[,:]' '{print $25 " " $41;}' modified_01.log | sort 

输出:

CLASS:CLASS1 10
CLASS:CLASS1 1
CLASS:CLASS2 9
CLASS:CLASS1 10
CLASS:CLASS2 17
CLASS:CLASS1 1

2 个答案:

答案 0 :(得分:2)

将输出传输到此awk:

awk -F '[: ]' '{a[$2]++; s[$2]+=$3} END{
        for (i in s) printf "%s%s%.1f\n", i, OFS, s[i]/a[i]}'
CLASS1 5.5
CLASS2 13.0

答案 1 :(得分:0)

将输入传递给下面的命令。然后你将得到所需的结果。

awk '{if(a[$1]){a[$1]=a[$1]+$2; count[$1]=count[$1]+1}else {a[$1]=$2;count[$1]=1}}END{for (i in a)print i"\t"a[i]/count[i];}' | cut -f2 -d":"