Reputation: 11
this is my file
air1.txt
fc:75:16:d2:91:a3 -90 targol
78:54:2e:7f:e8:9e -88 DLink
fc:75:16:d2:91:a3 -89 targol
78:54:2e:7f:e8:9e -89 DLink
78:54:2e:7f:e8:9e -88 DLink
78:54:2e:7f:e8:9e -87 DLink
fc:75:16:d2:91:a3 -90 targol
I want to calculate the average of second column for each name in the third column! Here is my scrip!
RSSI=$(awk '{print $3}' air1.txt | sort -u | while read line; do awk < air1.txt '{print $2,$3}' | grep $line | ./rssiMean.sh |cut -d'.' -f1 |awk '{print $line,$1}' ;done)
echo $RSSI
but the result is
-88 -88 -89 -89
Why I can't get $line
?!
BTW ./rssiMean.sh calculate the average!
Upvotes: 0
Views: 1183
Reputation: 41460
This should do:
awk '{a[$3]+=$2;b[$3]++} END {for (i in a) print i,a[i]/b[i]}' air1.txt
DLink -88
targol -89.6667
It sum up number for every data in column #3 and divide it by number of hits.
Upvotes: 3
Reputation: 26667
You cannot use bash variables as such in awk
script.
Rather use a awk variable and assign the value using the -v
paramter
eg:
$var=123
$awk -v awkvar=$var '{print awkvar}'
here awkvar
is an awk varible created and passed with value of $var
to make this change in your script
RSSI=$(awk '{print $3}' air1.txt | sort -u | while read line; do awk < air1.txt '{print $2,$3}' | grep $line | ./rssiMean.sh |cut -d'.' -f1 |awk -v line=$line '{print line,$1}' ;done)
echo $RSSI
The change made is
awk -v line=$line '{print line,$1}
awk varible line
is assigned with value of bash varibale $line
Upvotes: 0