16

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?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Wael
  • 1,533
  • 4
  • 20
  • 35

1 Answers1

24

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598