开关/箱不工作的awk [英] switch/case doesn't work in awk
问题描述
我写在RedHat Linux上一个简单的awk,却发现交换机/案例并不为我工作。我搜索的网页,但没有找到一个解决方案。以下是我的code:
BEGIN {
富= 1;
开关(富){
案例3:
打印×;
打破;
案例2:
打印Y;
打破;
情况1:
打印Z;
打破;
默认:
打印默认;
}
}
我运行awk的是GNU awk的3.1.5。我得到了以下错误:
AWK -f test.awk
块引用>AWK:test.awk:3:开关(富){
AWK:test.awk:3:^语法错误
AWK:test.awk:5:3的情况下:
AWK:test.awk:5:^语法错误
AWK:test.awk:8:2的情况下:
AWK:test.awk:8:^语法错误
AWK:test.awk:11:1的情况下:
AWK:test.awk:11:^语法错误
AWK:test.awk:14:默认情况下:
AWK:test.awk:14:^语法错误任何人可以请帮助我吗?谢谢!
解决方案在GAWK手册说:
6.4.5 switch语句
请注意:本节介绍的gawk 3.1.3增加了一个实验性的功能。
它不是默认启用。要启用它,使用'--enable-开关选项
正在配置,并内置GAWK时进行配置。见第B.2.2 [附加
配置选项],269页,以了解更多信息。
switch语句允许前pression的评估和语句的执行
基于案例的匹配。 case语句被检查的顺序比赛,他们
限定。如果没有找到合适的情况下,默认部分被执行,如果提供的
块引用>哪个版本的
GAWK
您使用的是?当时它与编译 - 使开关
选项如果您无法判断
GAWK
编译时使用- 使开关
和你得到的语法误差,这是合理的推断它不是。我使用呆子3.1.8
编译默认配置并获得pretty多正是你看到你的脚本错误。鉴于这种情况,这是极不可能的,你的版本是必要的配置选项编译。这并不难,如果你想重新编译GAWK
的选项。I am writing a simple awk in redhat linux,but found switch/case doesn't work for me. I searched on web, but didn't find a solution. The following is my code:
BEGIN { foo = 1; switch (foo) { case 3: print "x"; break; case 2: print "y" ; break; case 1: print "z" ; break; default: print "default" ; } }
the awk I am running is GNU Awk 3.1.5. I got the following err:
awk -f test.awk
awk: test.awk:3: switch (foo) { awk: test.awk:3: ^ syntax error awk: test.awk:5: case 3: awk: test.awk:5: ^ syntax error awk: test.awk:8: case 2: awk: test.awk:8: ^ syntax error awk: test.awk:11: case 1: awk: test.awk:11: ^ syntax error awk: test.awk:14: default: awk: test.awk:14: ^ syntax error
can anybody please help me out? thank you!
解决方案The GAWK manual says:
6.4.5 The switch Statement
NOTE: This subsection describes an experimental feature added in gawk 3.1.3. It is not enabled by default. To enable it, use the ‘--enable-switch’ option to configure when gawk is being configured and built. See Section B.2.2 [Additional Configuration Options], page 269, for more information.
The switch statement allows the evaluation of an expression and the execution of statements based on a case match. Case statements are checked for a match in the order they are defined. If no suitable case is found, the default section is executed, if supplied.
Which version of
gawk
are you using? Was it compiled with the--enable-switch
option?If you can't tell whether
gawk
was compiled with--enable-switch
and you are getting syntax errors, it is reasonable to infer that it is not. I'm usinggawk 3.1.8
compiled with the default configuration and get pretty much exactly the errors you are seeing on your script. Given that, it is highly unlikely that your version is compiled with the necessary configuration option. It isn't hard to recompilegawk
with the option if you want to.这篇关于开关/箱不工作的awk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!