"BEGIN blocks must have a part of the action" in awk script

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 a part of the action

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

+7
bash awk
source share
1 answer

This is because your open brace is on the next line.

So you need to write BEGIN { ... as follows:

 BEGIN { print "Processing file: " filename } 

Please also note 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 you can skip them.

+13
source share

All Articles