bash awk 脚本中的“BEGIN blocks must have an action part”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27776583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:07:13  来源:igfitidea点击:

"BEGIN blocks must have an action part" error in awk script

bashawk

提问by Wael

Here is my code:

这是我的代码:

#!/bin/sh

filename=$(/usr/bin/find -name "INSTANCE-*.log")
echo "filename is " $filename

awk '
 BEGIN
 {   
   print "Processing file: " filename 
 }

 {
  if(
BEGIN {
print "Processing file: " filename 
}
~ /Starting/) { print "The bill import has been Started on " " " } }' $filename > report.txt

When I execute it I get the following error:

当我执行它时,我收到以下错误:

BEGIN blocks must have an action part

BEGIN 块必须有一个动作部分

My BEGINblock has a printstatement so it has an action part. What am I missing here?

我的BEGIN块有一个print声明,所以它有一个动作部分。我在这里错过了什么?

回答by fedorqui 'SO stop harming'

This happens because your opening curly brace is in the next line.

发生这种情况是因为您的左花括号在下一行。

So what you need to do is to write BEGIN { ...like this:

所以你需要做的是这样写BEGIN { ...

/Starting/ {print "The bill import has been Started on " " " }


Note also that the main block can be rewritten to:

另请注意,主块可以重写为:

##代码##

That is, if ()and $0are implicit so they can be skipped.

也就是说,if ()$0是隐式的,因此可以跳过它们。