从具有重复地址的行获取平均值

时间:2015-07-20 09:09:02

标签: bash shell sed tshark

我编写了一个运行tshark的bash脚本,并将结果输出到日志文件中。我把它修剪下来只显示MAC地址和天线强度。 (试图用它来计算房间/建筑物中的人数)

它的输出如下:

c8:85:50:xx:xxxx    -88,-92
d8:fc:93:xx:xxxx    -76,-76
d8:fc:93:xx:xxxx    -76,-76
d8:fc:93:xx:xxxx    -76,-76
7c:c5:37:xx:xxxx    -69,-69
7c:c5:37:xx:xxxx    -67,-67
80:e6:50:xx:xxxx    -86,-86
d8:fc:93:xx:xxxx    -77,-77
d8:fc:93:xx:xxxx    -77,-77
d8:fc:93:xx:xxxx    -79,-79
34:e2:fd:xx:xxxx    -82,-82
34:e2:fd:xx:xxxx    -82,-82
a0:f3:c1:xx:xxxx    -49,-49
a0:f3:c1:xx:xxxx    -61,-61
80:be:05:xx:xxxx    -75,-75
80:be:05:xx:xxxx    -75,-75
80:be:05:xx:xxxx    -77,-77
80:be:05:xx:xxxx    -76,-76
80:be:05:xx:xxxx    -80,-80
a0:f3:c1:xx:xxxx    -49,-49
a0:f3:c1:xx:xxxx    -59,-59
80:e6:50:xx:xxxx    -88,-88
f8:16:54:xx:xxxx    -61,-61
f8:16:54:xx:xxxx    -61,-61
34:e2:fd:xx:xxxx    -81,-81
34:e2:fd:xx:xxxx    -82,-82

如您所见,某些信号会多次发送。我想得到每MAC地址的平均值。我该怎么做呢?

所以这个:

a0:f3:c1:xx:xxxx    -49,-49
a0:f3:c1:xx:xxxx    -59,-59

应该成为这个:

a0:f3:c1:xx:xxxx    -54,-54

2 个答案:

答案 0 :(得分:1)

一个选项是使用awk。使用以下内容创建解析文件:

BEGIN {} {    
print "Processing mac " $1 " with values " $2 
split($2, inputArray, ",")
strvalue = mapMac[$1];

if(strvalue != null){
    split(strvalue, value,",")
    print "    Current value for " $1 " is " strvalue;
    value[1] += inputArray[1];
    value[2] += inputArray[2];
    value[3]++;
}else{
    value[1] = inputArray[1];
    value[2] = inputArray[2];
    value[3] = 1;
}
strvalue = value[1]","value[2]","value[3];
print "    New value for " $1 " is " strvalue;
mapMac[$1] = strvalue;

}
END{ 
    for(item in mapMac){
        split(mapMac[item], value, ",")
        print item " ---> " value[1]/value[3] ", " value[2]/value[3]
    }
}

假设您的输入文件名为input.txt,请按以下方式调用awk:

awk -f parse input.txt

您将获得每个mac地址的平均值:

a0:f3:c1:xx:xxxx ---> -54.5, -54.5
7c:c5:37:xx:xxxx ---> -68, -68
d8:fc:93:xx:xxxx ---> -76.8333, -76.8333
c8:85:50:xx:xxxx ---> -88, -92
...

答案 1 :(得分:0)

#!/bin/bash

sort -k 1 file | tr -s " " > output

mac="";
sig1total=0;
sig2total=0;
count=0;
while read -r line; do
    oldmac=$mac;
    mac=$(echo $line | cut -d " " -f1);
    signal1=$(echo $line | cut -d " " -f2 | cut -d "," -f1);
    signal2=$(echo $line | cut -d " " -f2 | cut -d "," -f2);
#   echo "$mac : $signal1 : $signal2";      
    #if true, compute the average of the previous mac addresses
    if [ "$oldmac" != "$mac" ] && [ $count -gt 0 ] ; then
            sigavg1=$(echo "$sig1total / $count" | bc -lq);
            sigavg2=$(echo "$sig2total / $count" | bc -lq);
            echo "$oldmac $sigavg1,$sigavg2";
            sig1total=0;
            sig2total=0;
            count=0;
    fi

    sig1total=$(( $sig1total + $signal1 ));
    sig2total=$(( $sig2total + $signal2 ));
    count=$(( $count + 1 ));

done < output

我期待一个名为&#34; file&#34;的文件使用您在上面提供的格式,它会修剪额外的空间和按mac地址对其进行排序。然后我读取每一行,如果mac地址与我在前一行看到的mac地址相同,我输出旧的mac地址并计算总和。否则,我只是在当前读取的行中添加信号的值,然后递增计数。

结果输出是:

34:e2:fd:xx:xxxx -81.75000000000000000000,-81.75000000000000000000
7c:c5:37:xx:xxxx -68.00000000000000000000,-68.00000000000000000000
80:be:05:xx:xxxx -76.60000000000000000000,-76.60000000000000000000
80:e6:50:xx:xxxx -87.00000000000000000000,-87.00000000000000000000
a0:f3:c1:xx:xxxx -54.50000000000000000000,-54.50000000000000000000
c8:85:50:xx:xxxx -88.00000000000000000000,-92.00000000000000000000
d8:fc:93:xx:xxxx -76.83333333333333333333,-76.83333333333333333333

请注意,您可能需要安装bc,但其他所有内容都应包含在GNU核心工具中。