title: BEGIN and END block in groovy like in awk
body: <p>At <a href="https://issues.apache.org/jira/browse/GROOVY-1512" rel="nofollow noreferrer">https://issues.apache.org/jira/browse/GROOVY-1512</a>, a patch is available for executing BEGIN and END methods similar to awk or Perl. This could be useful for shell piping scenarios such as summing a list of numbers.</p>
  <p>I tried various syntaxes using the current version of Groovy, but it does not execute. Can somebody tell me what the correct syntax is and provide an example for it?</p>
  <p>To rephrase the question in detail. If I have the following,</p>
  <pre><code>my-desktop# du -s * | cut -f 1
  
  4
  1976
  4
  16
  16
  24
  16
  16
  16
  16
  16
  524
  20
  16
  20
  20
  4
  4
  4
  4
  364
  2356
  4
  5992
  28
  8
  </code></pre>
  <p>I want something like the following (inspired from awk) that would print its sum:</p>
  <pre><code>du -s * | cut -f 1 | groovy -a -n -e 'def sum; BEGIN{sum =0; }END {println sum;}sum=sum+split[0].toInteger()'
  </code></pre>
  <p>If the special BEGIN and END closures or functions are not implemented yet, then how do I print a sum of all the list of numbers piped in from other Unix commands?</p>