问题
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