bash脚本awk的if语句 [英] Bash Script Awk if statements

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

问题描述

给定的输入文件,记录格式...
(ID#,姓,名,Score1,Score2,Score3,得分(N)..
重要提示:将有大量的文件记录(不只是1行)

Given a input file with record format... (ID #, First name, Last name, Score1, Score2, Score3, Score(N).. Important note: There will be numerous records in file (not just 1 row).

我希望能够提供输出格式,如..
(ID#,平均得分,对应平均得分信级)

I would like to be able to provide output format such as.. (ID #, Average Score, Grade letter corresponding to average score)

输入示例:

900123123 Joe Brown 100 90 80
900900900 Tyler Black 80 95 75
900231231 Stephen Williams 70 75 80
900111111 Andrew Smith 85 75 90

输出的例子:

900123123 90 A
900900900 83.3 B
900231231 75 C
900111111 83.3 B

我的问题是if语句来确定分配什么档次的信。这是我目前的code:

My issue is the if-statements to determine what grade letter to assign. Here is my current code:

#!/bin/bash
awk '
BEGIN {FS=OFS=" "}
{
sum=0; average=0
for(i=3;i<=NF;i++)
        {sum+=$i;}
average = (sum/(NF-3))
if (average -ge 90)
        print $1, $2, $3, average, " A";
else if(average -ge 80 && -le 90)
        print $1, $2, $3, average, " B";
else if(average -ge 70 && -le 80)
        print $1, $2, $3, average, " C";
else if(average -ge 60 && -le 70)
        print $1, $2, $3, average, " D";
else
        print $1, $2, $3, average, "F";
}' grades.txt

这将导致输出:

900123123 Joe Brown 90  A
900323323 Tyler Black 83.3333  A
900231231 Stephen Williams 75  A
900232232 Andrew Smith 83.3333  A
   0  A

为什么第一个if语句被击中每次即使平均值小于90?我们知道这是因为它使用了print语句里,打印出一张带有任何数字。
另外,我不知道为什么 0 输出什么这样做的原因可能是。

Why is the first if statement being hit every time even when average is less than 90? We know this because it is used inside of the print statement and prints out A with any number. Also, I have no idea why 0 A is output and what the cause of this may be.

推荐答案

您第一条件是:

if (average -ge 90)

由于@thatotherguy poined出来, -ge 是一个外壳结构和AWK等价于&GT; = 。为了您的awk声明全文:

As @thatotherguy poined out, -ge is a shell construct and the equivalent in awk is >=. To awk your statement reads as:

if ( (average - ge) 90 )

这是说:减去命名未分配变量的数值 GE (即零)从变量平均然后将字符串 90 来的结果,所以如果平均为 70 ,例如那么它会读取如:

which is to say: subtract the numeric value of the unassigned variable named ge (i.e. zero) from the variable average and then concatenate the string 90 to the result so if average was 70, for example then it'd read as:

if ( (70 - 0) 90 )

该减法后:

if ( 70 90 )

其中置后为:

if ( 7090 )

这是一个真正的条件,所以你的脚本会一直执行,因为无论什么减法的结果你总是要与来concanetate它code的后续行90 和非零和非空的结果而告终。

which is a true condition and so you're script will always execute the subsequent line of code since no matter what the result of the subtraction is you're always going to concanetate it with 90 and end up with a non-zero and non-null result.

0 在你的输出的到底是几乎可以肯定,因为你必须在你的输入文件的末尾一个空行。您可以通过测试防止了 NF 之前进入操作块。

The 0 A at the end of your output is almost certainly because you have a blank line at the end of your input file. You can guard against that by testing NF before entering the action block.

下面是关于如何编写脚本一个建议:

Here's a suggestion on how to write your script:

awk 'NF {
    sum=0
    for(i=3;i<=NF;i++)
        sum+=$i
    average = sum/(NF-3)
    if      (average >= 90) grade = "A"
    else if (average >= 80) grade = "B"
    else if (average >= 70) grade = "C"
    else if (average >= 60) grade = "D"
    else                    grade = "F"
    print $1, $2, $3, average, grade
}' grades.txt

有对&放不点测试;&安培;平均&LT; 90 当你在所测试的条件的其他部分平均&GT; = 90

There's no point testing for && average < 90 when you're in the else part of a condition that tested for average >= 90.

这篇关于bash脚本awk的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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