我有一个像这样的文本文件
Experiment Replica Module Obs
General0 0 scenario.host[12].wlan.mac 189
General0 0 scenario.host[4].wlan.mac 1109
General0 0 scenario.host[2].wlan.mac 1250
General0 0 scenario.host[0].wlan.mac 1150
General0 0 scenario.host[6].wlan.mac 5636
General0 0 scenario.host[102].wlan.mac 16826
General0 0 scenario.rsu.wlan.mac 41030
我将计算“scenario.rsu.wlan.mac”之后列中的数字总和
用这个脚本
#!/bin/bash
INPUT_FILES=$1
EXPERIMENTS=$2
REPLICAS=$3
if [ -z "$INPUT_FILES" ]
then
echo "usage: $0 input data file.data (willcards allowed)"
fi
echo "Experiment Replica Mean"
find . -name "$INPUT_FILES" | while read file
do
export MODULE=`echo $file | cut -d- -f 2`
MODULE=${MODULE/.data/}
for EXP in $EXPERIMENTS;
do
for REP in $REPLICAS;
do
DATA=`cat "$file" | grep General$EXP | awk -v replica=$REP 'begin {sum=0;n=0} {if ($2 == replica && $3 == "scenario.rsu.wlan.mac") { sum+=$4;n+=1} } END {mean=sum/n; print mean}'`
MEAN=`echo $DATA | awk '{print $1}'`
echo "General$EXP $REP $MEAN"
done
done
done
请注意,当我想计算所有第四列但我无法选择包含“scenario.rsu.wlan.mac”的行时,代码运行良好。