You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2019/08/16 23:34:00 UTC

[jira] [Commented] (GROOVY-1512) When executing command-line scripts it should be possible to execute a BEGIN and an END method (akin to AWK or Perl)

    [ https://issues.apache.org/jira/browse/GROOVY-1512?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16909484#comment-16909484 ] 

Paul King commented on GROOVY-1512:
-----------------------------------

Up to date example tested using Groovy 3.0.0-beta-3:
{code}
$ bin/groovy -a -ne "def begin() { c = 0 }
def end() { println c }
c += split[1].toInteger() + split[-2].toInteger()" test.txt
{code}
Gives {{42}} using this test.txt:
{noformat}
12
123
1234
12345
123456
1234567
12345678
{noformat}


> When executing command-line scripts it should be possible to execute a BEGIN and an END method (akin to AWK or Perl)
> --------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-1512
>                 URL: https://issues.apache.org/jira/browse/GROOVY-1512
>             Project: Groovy
>          Issue Type: New Feature
>    Affects Versions: 1.0-JSR-6
>            Reporter: Joachim Baumann
>            Assignee: Guillaume Delcroix
>            Priority: Minor
>             Fix For: 1.8.1, 1.9-beta-1
>
>         Attachments: GROOVY-1512.patch, GroovyMainAll.patch
>
>
> When executing a command-line script e.g., via -pe it would sometimes be very useful to define a method BEGIN and a method END which are executed exactly once, BEGIN before the loop starts, and END after the loop has ended for all files to be processed. An example:
> groovy -a -ne "def BEGIN(){c=0};def END(){println c};c += split[0].toInteger()+split[-2].toInteger()" test.txt
> results in:
>  39 
> for the following test.txt:
> 1 2 3 4
> 1 2 3 4 5
> 1 2 3 4 5 6
> 1 2 3 4 5 6 7
> 1 2 3 4 5 6 7 8
> 1 2 3 4 5 6 7 8 9
> The patch provides this functionality and additionally that of GROOVY-1507 and GROOVY-1508. If a singular patch is necessary, I'll be happy to provide it.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)