“BEGIN blocks must have an action part” error in awk script

╄→гoц情女王★ 提交于 2020-05-25 11:24:19

问题


Here is my code:

#!/bin/sh

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

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

 {
  if($0 ~ /Starting/) 
  { 
    print "The bill import has been Started on "$1 " " $2
  }

}'  $filename > report.txt

When I execute it I get the following error:

BEGIN blocks must have an action part

My BEGIN block has a print statement so it has an action part. What am I missing here?


回答1:


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 {
print "Processing file: " filename 
}

Note also that the main block can be rewritten to:

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

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



来源:https://stackoverflow.com/questions/27776583/begin-blocks-must-have-an-action-part-error-in-awk-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!