如何解决awk,在健全性检查时跳过当前规则
如何在完整性检查失败时跳过当前的 awk 规则?
{
if (not_applicable) skip;
if (not_sanity_check2) skip;
if (not_sanity_check3) skip;
# the rest of the actions
}
恕我直言,以这种方式编写代码比,
{
if (!not_applicable) {
if (!not_sanity_check2) {
if (!not_sanity_check3) {
# the rest of the actions
}
}
}
}
1;
我需要跳过当前规则,因为我在最后有一个捕获所有规则。
更新,我正在尝试解决的案例。
文件中有多个匹配点需要匹配和更改,但是,我没有其他明显的迹象可以匹配我想要的内容。 嗯...,让我以这种方式简化它,我想匹配和更改第一个匹配项并跳过其余匹配项并按原样打印它们。
解决方法
据我了解您的要求,您在这里寻找 if
,else if
。您也可以使用较新版本的 gawk
软件包中提供的 switch
案例。
让我们在这里举一个 Input_file 的例子:
cat Input_file
9
29
以下是此处的 awk
代码:
awk -v var="10" '{if($0<var){print "Line " FNR " is less than var"} else if($0>var){print "Line " FNR " is greater than var"}}' Input_file
打印如下:
Line 1 is less than var
Line 2 isgreater than var
因此,如果您仔细查看代码,则会对其进行检查:
- 第一个条件,如果当前行小于 var 则它将在
if
块中执行。 - else if 块中的第二个条件,如果当前行大于 var 则在那里打印。
我真的不确定您要做什么,但如果我只关注您的问题 I want to match & alter the first match and skip the rest of the matches and print them as-is.
中的最后一句话……这就是您要尝试做的吗?
{ s=1 }
s && /abc/ { $0="uvw"; s=0 }
s && /def/ { $0="xyz"; s=0 }
{ print }
例如借用@Ravinder's example:
$ cat Input_file
9
29
$ awk -v var='10' '
{ s=1 }
s && ($0<var) { $0="Line " FNR " is less than var"; s=0 }
s && ($0>var) { $0="Line " FNR " is greater than var"; s=0 }
{ print }
' Input_file
Line 1 is less than var
Line 2 is greater than var
我对 s
使用了布尔标志变量名称 sane
,因为您在问题中还提到了有关测试条件是健全性检查的内容,因此每个条件都可以读为 is the input sane so far and this next condition is true?
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。