文件中的awk bash平均计算 [英] awk bash avg calculation in file

查看:47
本文介绍了文件中的awk bash平均计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写unix命令以显示所有学科得分均大于50且平均得分大于或等于75的所有学生的名次,平均名次和平均得分.

write the unix command to display roll, name and avg of all students whose score is more than 50 in each subject and average is more than or equal to 75.

avg可以计算为(subj_1 + subj_2)/2.

avg can be calculated as (subj_1+subj_2)/2.

输入:

roll ,name,subScore1,subScore2
123,a,88,78
101,b,76,90
812,c,78,98

输出:

123 a 83
812 c 78

我的代码:

awk 'BEGIN {FS=',';OFS=' '} {if(NR>1 (&& $3>50 && $4>50) && ($3+$4)/2 >= 75){print $1,$2,($3+$4)/2}}' input_file

我不知道为什么我会出错.请帮助大家.

I don't know why I'm getting error. please help guys.

推荐答案

添加更多通用解决方案,其中OP的Input_file可以具有4个以上字段/列情况一可以尝试遵循.

Adding more generic solution where OP's Input_file could have more than 4 fields/columns in that case one could try following.

awk '
BEGIN{
  FS=","
}
FNR==1{
  next
}
{
  for(i=3;i<=NF;i++){
    if($i>=50){
      ++count
    }
    sum+=$i
  }
  avg=(sum/count)
  if(count==(NF-2) && avg>=75){
    print $1,$2,avg
  }
  count=sum=avg=0
}
'   Input_file



您可以尝试使用GNU awk 来跟踪,编写和测试所示示例.



Could you please try following, written and tested with shown samples with GNU awk.

awk '
BEGIN{
  FS=","
}
FNR==1{
  next
}
avg=($3+$4)/2
avg>=75 && ($3>=50 && $4>=50){
  print $1,$2,avg
}
'  Input_file

这篇关于文件中的awk bash平均计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆