如何在一行或多行中查找具有相同值的行,并使用awk添加值

时间:2017-10-18 23:49:58

标签: linux shell awk scripting

我有以下数据:

    Name,Team,First Test, Second Test, Third Test
    Tom,Red,5,17,22
    Joe,Green,3,14,22
    Maria,Blue,6,18,21
    Fred,Blue,2,15,23
    Carlos,Red,-1,15,24
    Phuong,Green,7,19,21
    Enrique,Green,3,16,20
    Nancy,Red,9,12,24

我需要找到Green Teams Average。我在如何完成它方面遇到了一些麻烦。

到目前为止,我有:

awk '/Green/' teamlist.txt

给了我结果:

Joe,Green,3,14,22
Phuong,Green,7,19,21
Enrique,Green,3,16,20

但是,当我尝试打印以下声明时:

awk '/Green/ {print $3, $4, $5}' teamlist.txt

我刚收到一个空白输出,没有任何显示。 此外,我需要添加$ 3,$ 4和$ 5列,然后除以9。

到目前为止,这是我的代码:

#Begin. Start with the Field Separator using comma.
BEGIN {
FS=",";
}

#Middle. 
{
if (NR > 1)

{
name[NR] = $1;                       #Name record is field 1 
average[NR] = ($3 + $4 + $5) / 3;    #Average is number of records which are fields 3,4,5. Add them up and then divide by 3.

testOneAverage += $3                 #Average of test one. Keeps adding field three to itself for the number of records.

testTwoAverage += $4                 #Average of test two. Keeps adding field four to itself for the number of records.
testThreeAverage += $5               #Average of test three. Keeps adding field five to itself for the number of records.

}


}

#End
END {
print "Name         Average";
print "------       ------";

i = 1;

while (i <= FNR) {
   printf("%-10s %7.2f\n", name[i] , average[i++]);
}

if (NR > 0) {
   print "---------------------------";
   print "Average for Test 1: " testOneAverage /  8;
   print "Average for Test 2: " testTwoAverage /8;
   print "Average for Test 3: " testThreeAverage /8;
}

2 个答案:

答案 0 :(得分:2)

你的短语:我需要找到绿色团队平均 + 添加$ 3,$ 4和$ 5列,然后除以 9 - 表示您想获得总体/总平均值。

Awk 解决方案:

awk -F',' 'NR>1 && $2=="Green"{ ++c; sum+=$3+$4+$5 }END{ print sum/(c*3) }'  file

输出:

13.8889

答案 1 :(得分:1)

没有打印的原因是你需要明确表示“,”是你的字段分隔符,如:

$ awk -F, '/Green/ {print $3, $4, $5}' teamlist.txt
3 14 22
7 19 21
3 16 20

接下来,你要总结所有列3,4和5并除以9.像这样的东西会这样做:

$ awk -F, '$2=="Green"{i+=3;t+=$3+$4+$5}END{printf "Average %.2f\n", t/i}' teamlist.txt
Average 13.89

把所有东西放在一起会让我们:

$ cat tst.awk
BEGIN { FS="," }
NR > 1{ 
    name[++i]=$1                     # save name
    avg[i]=($3+$4+$5)/3              # save avg of this name
    test1+=$3                        # sum up cols 3, 4, 5 resp.
    test2+=$4
    test3+=$5
    lines++                          # count nr of lines
    if ($2=="Green"){ 
       green+=$3+$4+$5               # for "Green" sum up the tests 
       gl++                          # remember how many green lines there are
    }
}
END {                          
   print "Name         Average";
   print "------       ------";
   for (i in name)
       printf("%-10s %7.2f\n", name[i] , avg[i])  # print avg's for each person
   printf "Average Test 1: %.2f\n", test1/lines   # print avg's per test
   printf "Average Test 2: %.2f\n", test2/lines
   printf "Average Test 3: %.2f\n", test3/lines
   printf "Average Green: %.2f\n", green/(gl * 3) # print avg for green
}

然后致电:

$ awk -f tst.awk teamlist.txt
Name         Average
------       ------
Tom          14.67
Joe          13.00
Maria        15.00
Fred         13.33
Carlos       12.67
Phuong       15.67
Enrique      13.00
Nancy        15.00
Average Test 1: 4.25
Average Test 2: 15.75
Average Test 3: 22.12
Average Green: 13.89