Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

Re: [groovy-user] Skip lines with transformLine

Armin Ehrenfels

2008-08-28

Replies: Find Java Web Hosting

Author LoginPost Reply
Tom Corcoran schrieb:
> I am wondering if there any way I am missing of skipping lines when using
> transformLine?
>
> The last line in the closure is the line value written. Looking at a SSCCE,
> we have file1.txt with 4 lines:
>
> line1
> line2
> line11
> line22
>
> I want to produce the file2.txt with 2 lines:
>
> line1
> line11
>
> import java.util.regex.Matcher
> import java.io.Reader
> import java.io.Writer
>
> def file1 = /C:\Workspace\Test\file1.txt/
> def file2 = /C:\Workspace\Test\file2.txt/
> def writeLine
> FileWriter writeFile = new FileWriter(file2)
>           new FileReader(file1).transformLine(writeFile) { line ->
>              Matcher m1 = line =~ /line1\d*/
>              writeLine = ''
>              if (m1.matches()) {
>                 writeLine = line            
>              }
>              writeLine
>           }
>
> will give:
>
> line1
> <blank line>
> line11
> <blank line>
>
> I could then reprocess the field to remove blank lines, but it would be
> great if there was a way of skipping lines with transformLine.
>  
In the closure, just initialize writeLine = null instead of writeLine = ''.

Regards

Armin

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.