Bash脚本来计算平均分数
我有一个有很多标记的文件。脚本的目标是能够在屏幕上打印每个学生的平均分数Bash脚本来计算平均分数,bash,shell,awk,Bash,Shell,Awk,我有一个有很多标记的文件。脚本的目标是能够在屏幕上打印每个学生的平均分数 Charles:1:8:9 Daniel:1:3:3 Josh:4:6:1 Alfonso:7:5:1 Eric:6:8:5 我尝试了以下代码: cat /home/sysadmin/MARKS.txt | while read line do # These extracts the 3 marks of each student. mark1=`cat /home/sysadmin/MARKS.txt | c
Charles:1:8:9
Daniel:1:3:3
Josh:4:6:1
Alfonso:7:5:1
Eric:6:8:5
我尝试了以下代码:
cat /home/sysadmin/MARKS.txt | while read line
do
# These extracts the 3 marks of each student.
mark1=`cat /home/sysadmin/MARKS.txt | cut -d":" -f2`
mark2=`cat /home/sysadmin/MARKS.txt | cut -d":" -f3`
mark3=`car /home/sysadmin/MARKS.txt | cut -d":" -f4`
# Calculate average.
let add=$mark1+$mark2+$mark3
let avgMark=$add/3
# Print average.
echo $avgMark
但在屏幕中,脚本仅在每个学生中返回0,如下所示:
0
0
0
0
0
如有任何帮助,我们将不胜感激,谢谢!!新年快乐 如果您对
awk
没有问题(我们不需要在一个循环中使用这么多命令),您可以尝试以下操作,可以在单个awk
本身中完成
awk 'BEGIN{FS=":"} {print "Average for student "$1 " is: " ($2+$3+$4)/3}' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS=":" ##Setting FS as colon here.
}
{
print "Average for student "$1 " is: " ($2+$3+$4)/3
##printing student name(1st column) and printing avg(adding 2nd, 3rd and 4th column) and dividing it with 3 here.
}
' Input_file ##Mentioning Input_file name here.
如果您对
awk
(我们不需要在一个循环中使用这么多命令)没有问题,您可以尝试以下操作,可以在单个awk
本身中完成
awk 'BEGIN{FS=":"} {print "Average for student "$1 " is: " ($2+$3+$4)/3}' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS=":" ##Setting FS as colon here.
}
{
print "Average for student "$1 " is: " ($2+$3+$4)/3
##printing student name(1st column) and printing avg(adding 2nd, 3rd and 4th column) and dividing it with 3 here.
}
' Input_file ##Mentioning Input_file name here.
使用bash和数组($col
):
#/bin/bash
declare-i av#set integer属性
而IFS=“:”读取-r-a列;做
av=(${col[1]}+${col[2]}+${col[3]})/3
回显“${col[0]}:$av”
完成
输出:
Charles: 6
Daniel: 2
Josh: 3
Alfonso: 4
Eric: 6
查尔斯:6
丹尼尔:2
乔希:3
阿方索:4
埃里克:6
请参阅:
帮助阅读带有bash和数组($col
)的:
#/bin/bash
declare-i av#set integer属性
而IFS=“:”读取-r-a列;做
av=(${col[1]}+${col[2]}+${col[3]})/3
回显“${col[0]}:$av”
完成
输出:
Charles: 6
Daniel: 2
Josh: 3
Alfonso: 4
Eric: 6
查尔斯:6
丹尼尔:2
乔希:3
阿方索:4
埃里克:6
请参阅:帮助阅读
这里有一个Perl
:
$ perl -F: -lanE '$sum=0; for $i (1 .. $#F)
{$sum+=$F[$i]}
printf "%s: %.2f\n", $F[0], $sum/$#F;' file
Charles: 6.00
Daniel: 2.33
Josh: 3.67
Alfonso: 4.33
Eric: 6.33
或者,也许是一个更有趣的输出:
$ perl -F: -lanE '$sum=0; for $i (1 .. $#F)
{$sum+=$F[$i]}
printf "%-10s%.2f average with %i scores\n",
$F[0].":", $sum/$#F, $#F;' file
这也是您可以在awk
中执行的方法:
$ awk -F: '{sum=0
for (i=2; i<=NF; i++) sum+=$i
printf "%-10s%.2f average with %i scores\n",
($1 ":"), sum/(NF-1), NF-1}' file
下面是一个Perl
:
$ perl -F: -lanE '$sum=0; for $i (1 .. $#F)
{$sum+=$F[$i]}
printf "%s: %.2f\n", $F[0], $sum/$#F;' file
Charles: 6.00
Daniel: 2.33
Josh: 3.67
Alfonso: 4.33
Eric: 6.33
或者,也许是一个更有趣的输出:
$ perl -F: -lanE '$sum=0; for $i (1 .. $#F)
{$sum+=$F[$i]}
printf "%-10s%.2f average with %i scores\n",
$F[0].":", $sum/$#F, $#F;' file
这也是您可以在awk
中执行的方法:
$ awk -F: '{sum=0
for (i=2; i<=NF; i++) sum+=$i
printf "%-10s%.2f average with %i scores\n",
($1 ":"), sum/(NF-1), NF-1}' file
你能试着回显中间变量来找出发生了什么吗?你能试着回显中间变量来找出发生了什么吗?