AWK脚本了解给定文本文件有多少次考试 [英] AWK script to know how many exams there are with given text file

查看:33
本文介绍了AWK脚本了解给定文本文件有多少次考试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建标题行才能组织一些考试.我不知道有多少次考试.我知道我必须使用NR == 1和NF来确定第一行中有多少个字段,但是我不知道如何将其放入字符串中.

I have to create a header line to organize some exams. I do not know how many exams there have been. I understand I have to use NR == 1 and NF to determine how many fields there are in the first line, but I do not know how to put it into a string.

文本文件是

Steve   Krause      67  96  78
Will  Brown  78  62  86
Andrew  Phillips   63  88  79

我需要这样的字符串

Name                     Exam 1 Exam 2 Exam 3 Exam 4 Average

我正在努力格式化第一个字符串,而没有A)在不循环的情况下知道有多少个考试(领域),并且B)获得每个学生的平均成绩.

I am struggling to format the first string without A)knowing how many exams(fields) there are without looping through and B) getting the averages for each student.

这就是我所拥有的,因为我知道只有3项考试.

This is what I have because I know there are only 3 exams.

#!/usr/bin/awk -f                                                                         
                                                                                          
{                                                                                         
    printf("%s \t %28s %7s %7s %7s %7s\n", 
        "Name", "Exam 1", "exam 2", "Exam 3", "Exam 4", "Average");                                                                             
                                                                                          
    examSTUAVG = ($3 + $4 + $5) / 4;                                                      
                                                                                          
    printf("%s \t %28s %7s %7s %7s %7s\n", $1, "0", $3, $4, $5,examSTUAVG);               
                                                                                          
    for(i = 1; i <= NF; i++){                                                             
        averages[i] += $(i) / (NF -2);                                                    
        print(averages[i])                                                                
    }                                                                                     
}    

所需的输出:

Name                            Exam 1 Exam 2 Exam 3 Exam 4 Average

Steve                               0     67     96     78     60.3
Will                                0     78     62     86     56.5 
Andrew                              0     63     88     79     57.5

推荐答案

$ cat tst.sh
#!/usr/bin/env bash

awk '
BEGIN { OFS="\t" }
NR == 1 {
    hdr = "Name" OFS "Exam 1"
    for (i=3; i<=NF; i++) {
        hdr = hdr OFS "Exam " (i-1)
    }
    print hdr OFS "Average"
}
{
    row = $1 " " $2 OFS 0
    tot = 0
    cnt = 1
    for (i=3; i<=NF; i++) {
        row = row OFS $i
        tot += $i
        cnt++
    }
    printf "%s%s%0.1f\n", row, OFS, tot / cnt
}
' "${@:--}"

$ ./tst.sh file | column -s$'\t' -t
Name             Exam 1  Exam 2  Exam 3  Exam 4  Average
Steve Krause     0       67      96      78      60.2
Will Brown       0       78      62      86      56.5
Andrew Phillips  0       63      88      79      57.5

请注意, sprintf 的舍入方式取决于平台,但是在许多系统中,它使用无偏舍入,因此舍入为偶数(因此60.25变为60.2,而不是上面的60.3).如果这是不可接受的,则可以使用多种方法来实现其他类型的舍入(例如向上或向下),请参见例如https://www.gnu.org/software/gawk/manual/gawk.html#Round-Function .

Note that how sprintf does rounding is platform-dependent but in many systems it uses unbiased rounding and so rounds towards even numbers (hence 60.25 becoming 60.2 instead of 60.3 above). If that's unacceptable there are ways to implement other types of rounding (e.g. up or down), see https://www.gnu.org/software/gawk/manual/gawk.html#Round-Function for example.

这篇关于AWK脚本了解给定文本文件有多少次考试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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