awk - selecting some columns with grep -
i have text file this
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
and going calculate sum of numbers in column after "scenario.rsu.wlan.mac"
with script
#!/bin/bash input_files=$1 experiments=$2 replicas=$3 if [ -z "$input_files" ] echo "usage: $0 input data file.data (willcards allowed)" fi echo "experiment replica mean" find . -name "$input_files" | while read file export module=`echo $file | cut -d- -f 2` module=${module/.data/} exp in $experiments; rep in $replicas; 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
notice code work when want calculate fourth column cannot select rows contain "scenario.rsu.wlan.mac".
you input file has 1 row containing scenario.rsu.wlan.mac
use following demostrate:
$ cat file 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 general0 0 scenario.rsu.wlan.mac 34234 general0 0 scenario.rsu.wlan.mac 4453
just test if third field matches, sum fourth , print results in end
block:
$ awk '$3=="scenario.rsu.wlan.mac"{s=s+$4}end{print "sum:",s}' file sum: 79717
there lot of bad practices in script , question isn't 100% clear further clarification helpful.
Comments
Post a Comment