2017-07-14 132 views
0

我正在处理一些统计信息。我有一个小时的专栏,在这个小时内这个交易发生的次数和类型会显示多少次。awk合并具有相同值的行

00 28 INFO 
00 3 WARNING 
01 29 INFO 
01 8 WARNING 
01 1 ERROR 
...  

我需要有类似的东西:

hour INFO WARNING ERROR 
00 28 3  0 
01 29 8  1 
...  

错误行并不总是存在的,所以我需要一些(如果3 $ == “INFO” 当时)在那里。我知道这是关于数组的,但无法使其工作。

我会很感激你的帮助:)

+1

显示您的尝试 – anubhava

回答

1

我不能做到这一点在AWK,但我可以在bash解决它:

# init array with 0 
declare -A matrix 
for ((hour=0;hour<=23;hour++)) do 
    for ((type=0;type<=2;type++)) do 
     matrix[$hour,$type]=0 
    done 
done 

# read and assign values 
while read -r line 
do 
    hour=$(echo $line | cut -f1 -d-) 
    count=$(echo $line | cut -f2 -d-) 
    type=$(echo $line | cut -f3 -d-) 
    case $type in 
    INFO) 
     matrix[$hour,0]=$count 
    WARNING) 
     matrix[$hour,1]=$count 
    ERROR) 
     matrix[$hour,2]=$count 
done < "$filename" 

# output 
echo "HOUR\tINFO\tWARNING\tERROR" 
for ((hour=0;hour<=23;hour++)) do 
    echo "${hour}\t$matrix[$hour,0]\t$matrix[$hour,1]\t$matrix[$hour,2]" 
done 

我无处一个计算机附近。请原谅任何可能的小语法错误。