简体   繁体   English

在 awk 中嵌套 if 语句的正确语法是什么?

[英]What is the correct syntax for nesting if-statements in awk?

I'm writing a awk command and I would like to nest if-statements using the conditional block form:我正在编写一个 awk 命令,我想使用条件块形式嵌套 if 语句:

condition{actions}

instead of the one that uses the if keyword:而不是使用 if 关键字的那个:

if(condition){actions}

This because I'm trying to use as few characters as possible.这是因为我试图使用尽可能少的字符。

Using the if keyword it works:使用 if 关键字可以工作:

awk '{system("date +%s%N")}'|awk '{t[++b]=$1;}b>1{if(b>4)e=b-5;print int(6E10*(b-e)/(t[b]-t[e+1])),"bpm"}'

but when I try to do with the other way:但是当我尝试用另一种方式做时:

awk '{system("date +%s%N")}'|awk '{t[++b]=$1;}b>1{b>4{e=b-5;}print int(6E10*(b-e)/(t[b]-t[e+1])),"bpm"}'

I get a syntax error:我收到一个语法错误:

awk: lin. awk:林。 de com.:1: {b++;}b>1{b>4{e=b-5;}t[b]=$1;r=int(3E11/(t[b]-t[++e]));print r,"bpm"} de com.:1: {b++;}b>1{b>4{e=b-5;}t[b]=$1;r=int(3E11/(t[b]-t[++e] ));打印 r,"bpm"}

awk: lin. awk:林。 de com.:1:(......................)^ syntax error de com.:1:(......................)^ 语法错误

What would be the correct syntax?什么是正确的语法?

Edit: the command is for calculating how many beats per minute (bpm) a user makes when he/she presses the Enter key on the keyboard.编辑:该命令用于计算用户在按下键盘上的 Enter 键时每分钟产生多少节拍 (bpm)。

Input: to hit Enter key Output: how many bpm he/she did输入:按回车键输出:他/她做了多少 bpm

Example -- If I hit the Enter key at a rate of ~ 115 bpm, the answer could be more or less like this:示例——如果我以大约 115 bpm 的速度按下 Enter 键,答案可能或多或少是这样的:

120 bpm 120 次/分

112 bpm 112 次/分

119 bpm 119 次/分

122 bpm 122 次/分

Not sure I'm getting my point across in comments so ... consider this:不确定我是否在评论中表达了我的观点,所以......考虑一下:

awk '{system("date +%s%N")}'

can be written as just:可以写成:

date +%s%N

The other script:另一个脚本:

{b++;}b>1{if(b>4)e=b-5;t[b]=$1;r=int(3E11/(t[b]-t[++e]));print r,"bpm"}

when written out legibly is:当清楚地写出时是:

{
        b++
}

b > 1 {
        if (b > 4) {
                e = b - 5
        }
        t[b] = $1
        r = int(3E11 / (t[b] - t[++e]))
        print r, "bpm"
}

which can then obviously be rewritten more briefly as:然后显然可以更简短地重写为:

(++b) > 4 {
        e = b - 5
}
b > 1 {
        t[b] = $1
        r = int(3E11 / (t[b] - t[++e]))
        print r, "bpm"
}

and from that there's no obvious reason for either t[b] nor r to exist since you can just write:从那以后, t[b]r都没有明显的理由存在,因为你可以这样写:

(++b) > 4 {
        e = b - 5
}
b > 1 {
        print int(3E11 / ($1 - t[++e])), "bpm"
}

and that can further be reduced to:并且可以进一步简化为:

(++b) > 1 {
        e = (b > 4 ? b - 5 : e) + 1
        print int(3E11 / ($1 - t[e])), "bpm"
}

which you can then squeeze back into 1 line as you see fit.然后您可以根据需要将其挤回 1 行。 There's probably other ways to reduce the code but without an example to test against that's as far as Id want to guess at - there's probably already bugs introduced above by not having something to test against.可能有其他方法可以减少代码,但没有一个例子来测试,就我想猜测的而言 - 上面可能已经引入了错误,因为没有可以测试的东西。 Hopefully it gives you the idea of what you can do to reduce your code and why we need an example we can test against to be able to help you.希望它能让您了解如何减少代码,以及为什么我们需要一个可以测试的示例来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM