The marks of the students are given as a table in the following format Name | rollno | marks in exam1 | marks in exam 2 ... i.e. There is one record per line and each column is separated by a | (pipe) character.At the end of all the records I want to add extra lines which contains information about max, min mean...So my question is How would one add new record at the end of input file?
学生的标记以表格的形式给出,其格式为姓名| rollno |考试中的分数|考试2中的分数...即每行有一个记录,每列用|分隔(管道)character.At所有记录的结尾我想添加额外的行,其中包含有关max,min的信息...所以我的问题是如何在输入文件的末尾添加新记录?
Example: Here is a sample input
示例:这是一个示例输入
Piyush | 12345 | 5 | 5 | 4
James | 007 | 0 | 0 | 7
Knuth | 31415 | 100 | 100 | 100
For which the output is
输出为
Piyush | 12345 | 5 | 5 | 4 | 14
James | 007 | 0 | 0 | 7 | 7
Knuth | 31415 | 100 | 100 | 100 | 300
max | | 100 | 100 | 100 | 300
min | | 0 | 0 | 4 | 7
mean | | 35.00 | 35.00 | 37.00 | 107.00
sd | | 46.01 | 46.01 | 44.56 | 136.50
1 个解决方案
#1
0
awk '
BEGIN { FS=OFS="|" }
{
sum = 0
for (i=3;i<=NF;i++) {
tot[i] += $i
sum += $i
max[i] = ( (i in max) && (max[i] > $i) ? max[i] : $i )
}
print $0, sum
max[i] = ( (i in max) && (max[i] > sum) ? max[i] : sum )
}
END {
printf "max" OFS ""
nf = NF+1
for (i=3; i<=nf; i++) {
printf "%s%s", max[i], (i<nf?OFS:ORS)
}
}'
repeat for min and whatever else you need to calculate and check the printf formatting flags for whatever spacing you need, if any.
重复最小值以及您需要计算的任何其他内容,并检查printf格式化标志,以查找所需的任何间距(如果有)。
#1
0
awk '
BEGIN { FS=OFS="|" }
{
sum = 0
for (i=3;i<=NF;i++) {
tot[i] += $i
sum += $i
max[i] = ( (i in max) && (max[i] > $i) ? max[i] : $i )
}
print $0, sum
max[i] = ( (i in max) && (max[i] > sum) ? max[i] : sum )
}
END {
printf "max" OFS ""
nf = NF+1
for (i=3; i<=nf; i++) {
printf "%s%s", max[i], (i<nf?OFS:ORS)
}
}'
repeat for min and whatever else you need to calculate and check the printf formatting flags for whatever spacing you need, if any.
重复最小值以及您需要计算的任何其他内容,并检查printf格式化标志,以查找所需的任何间距(如果有)。