使用Bash脚本来查找文件的平均值 [英] Finding averages from reading a file using Bash Scripting

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

问题描述

我正在尝试编写一个读取文件'names.txt'的bash脚本,并将计算人物等级的平均值。例如,names.txt看起来像这样。

  900706845 Harry Thompson 70 80 90 
900897665 Roy Ludson 90 90 90

脚本应该读取该行,打印出该人的ID号,三个测试分数和相应的字母等级。所以输出需要像这样

  900706845 80 B 
900897665 90 A


$ b

 # !/ bin / bash 
cat names.txt | while read x
do
$ SUM = 0;因为我在'names.txt';做SUM = $(($ SUM + $ i));
完成;
$ b $ echo $ SUM / 3
完成

回声将只打印在这一点的平均数,但我试图让它至少计算平均值,然后我尝试其他部分以及。宝贝步骤!

解决方案

 #!/ bin / bash 
读取姓名1 name2 g1 g2 g3
do
avg = $(echo($ g1 + $ g2 + $ g3)/ 3 | bc)
echo $ a $ name1 $ name2 $ avg
完成< names.txt

输出:

<$ p $ 900706845 Harry Thompson 80
900897665 Roy Ludson 90


I am trying to write a bash script that reads a file 'names.txt' and will compute the average of peoples grades. For instance, names.txt looks something like this.

900706845 Harry Thompson 70 80 90
900897665 Roy Ludson 90 90 90 

The script should read the line, print out the ID# of the person, the average of the three test scores and the corresponding letter grade. So the output needs to look like this

900706845 80 B
900897665 90 A

Heres what I have

    #!/bin/bash
    cat names.txt | while read x
    do 
         $SUM=0; for i in 'names.txt'; do SUM=$(($SUM + $i));
         done;

         echo $SUM/3
    done

I understand the echo will only print out the averages at this point, but I am trying to atleast get it to compute the averages before I attempt the other parts as well. Baby steps!

解决方案

Like this maybe:

#!/bin/bash
while read a name1 name2 g1 g2 g3
do
   avg=$(echo "($g1+$g2+$g3)/3" | bc)
   echo $a $name1 $name2 $avg
done < names.txt

Output:

900706845 Harry Thompson 80
900897665 Roy Ludson 90

这篇关于使用Bash脚本来查找文件的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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