Skip lines with transformLine

View: New views
7 Messages — Rating Filter:   Alert me  

Skip lines with transformLine

by Tom Corcoran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

RE: Skip lines with transformLine

by Jürgen Hermann-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

you're not transforming lines, you're filtering them. look at

http://www.amath.net/perso/tmp/java/io/Reader.html

again. ;)

> -----Original Message-----
> From: Tom Corcoran [mailto:boardtc@...]
> Sent: Wednesday, August 27, 2008 6:33 PM
> To: user@...
> Subject: [groovy-user] Skip lines with transformLine
>
>
> 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.
> --
> View this message in context:
> http://www.nabble.com/Skip-lines-with-transformLine-tp19184756
> p19184756.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

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

    http://xircles.codehaus.org/manage_email



Re: Skip lines with transformLine

by Saager Mhatre :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Wouldn't Reader.filterLine do the job?

On Wed, Aug 27, 2008 at 10:02 PM, Tom Corcoran <boardtc@...> wrote:

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.
--
View this message in context: http://www.nabble.com/Skip-lines-with-transformLine-tp19184756p19184756.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

   http://xircles.codehaus.org/manage_email





--
Saager Mhatre

Re: Skip lines with transformLine

by Armin Ehrenfels :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



Re: Skip lines with transformLine

by Saager Mhatre :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

def input = new StringReader("""
line1
line2
line11
line22
""")

def output = System.out

output << input.filterLine { l -> l ==~ /line1\d*/ }

On Thu, Aug 28, 2008 at 4:06 PM, Saager Mhatre <saager.mhatre@...> wrote:
Wouldn't Reader.filterLine do the job?


On Wed, Aug 27, 2008 at 10:02 PM, Tom Corcoran <boardtc@...> wrote:

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.
--
View this message in context: http://www.nabble.com/Skip-lines-with-transformLine-tp19184756p19184756.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

   http://xircles.codehaus.org/manage_email





--
Saager Mhatre



--
Saager Mhatre

Re: Skip lines with transformLine

by Tom Corcoran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the posts everyone. I need to use transformLine, though my SSCCE did not suggest that, so filterLine will not work for me. Armin, Great idea initialising to null. Works well. Only issue I think is that an extra blank line is added to file2.txt. I then have to strip this as it later causes an import error for me....

Tom.

Armin Ehrenfels wrote:
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 = ''.

Re: Skip lines with transformLine

by Armin Ehrenfels :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Tom Corcoran schrieb:
> Thanks for the posts everyone. I need to use transformLine, though my SSCCE
> did not suggest that, so filterLine will not work for me. Armin, Great idea
> initialising to null. Works well. Only issue I think is that an extra blank
> line is added to file2.txt. I then have to strip this as it later causes an
> import error for me....
>
> Tom.
>
>  
Strange, doesn't happen to me. Is it appended after the last line matched ?

Armin

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

    http://xircles.codehaus.org/manage_email