Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Knocking Lines Out Of A Multiline String

Reply
Thread Tools

Knocking Lines Out Of A Multiline String

 
 
Andrew Stewart
Guest
Posts: n/a
 
      03-22-2007
Hello,

What's a (good!) way to remove lines matching a pattern from a
multiline string?

For example, I would like to remove lines matching /usr/local/lib
from the multiline string:

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/test_process.rb:382:in `process'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/test_process.rb:353:in `post'
test/functional/orders_controller_test.rb:241:in
`test_should_handle_errors_on_edit'

...to give:

test/functional/orders_controller_test.rb:241:in
`test_should_handle_errors_on_edit'

I tried matching the pattern-to-remove with gsub and substituting an
empty string, but that leaves me with lots of blank lines and not
really any nearer to the answer.

Thanks and regards,
Andy Stewart

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-22-2007
On 22.03.2007 15:43, Andrew Stewart wrote:
> What's a (good!) way to remove lines matching a pattern from a multiline
> string?
>
> For example, I would like to remove lines matching /usr/local/lib from
> the multiline string:
>
>
> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:382:in
> `process'
>
> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:353:in
> `post'
> test/functional/orders_controller_test.rb:241:in
> `test_should_handle_errors_on_edit'
>
> ..to give:
>
> test/functional/orders_controller_test.rb:241:in
> `test_should_handle_errors_on_edit'
>
> I tried matching the pattern-to-remove with gsub and substituting an
> empty string, but that leaves me with lots of blank lines and not really
> any nearer to the answer.


Convert it to an array and select like

>> "foo\nbar\n".to_a.select {|l| /^f/ =~ l}

=> ["foo\n"]

Kind regards

robert
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-22-2007
On 22.03.2007 16:09, Robert Klemme wrote:
> On 22.03.2007 15:43, Andrew Stewart wrote:
>> What's a (good!) way to remove lines matching a pattern from a
>> multiline string?
>>
>> For example, I would like to remove lines matching /usr/local/lib from
>> the multiline string:
>>
>>
>> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:382:in
>> `process'
>>
>> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:353:in
>> `post'
>> test/functional/orders_controller_test.rb:241:in
>> `test_should_handle_errors_on_edit'
>>
>> ..to give:
>>
>> test/functional/orders_controller_test.rb:241:in
>> `test_should_handle_errors_on_edit'
>>
>> I tried matching the pattern-to-remove with gsub and substituting an
>> empty string, but that leaves me with lots of blank lines and not
>> really any nearer to the answer.

>
> Convert it to an array and select like
>
> >> "foo\nbar\n".to_a.select {|l| /^f/ =~ l}

> => ["foo\n"]


Bullshit: just use select:

>> "foo\nbar\n".select {|l| /^f/ =~ l}

=> ["foo\n"]

Sorry for the noise.

robert
 
Reply With Quote
 
Leslie Viljoen
Guest
Posts: n/a
 
      03-22-2007
On 3/22/07, Andrew Stewart <(E-Mail Removed)> wrote:
> Hello,
>
> What's a (good!) way to remove lines matching a pattern from a
> multiline string?
>
> For example, I would like to remove lines matching /usr/local/lib
> from the multiline string:
>
> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
> action_controller/test_process.rb:382:in `process'
> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
> action_controller/test_process.rb:353:in `post'
> test/functional/orders_controller_test.rb:241:in
> `test_should_handle_errors_on_edit'
>
> ...to give:
>
> test/functional/orders_controller_test.rb:241:in
> `test_should_handle_errors_on_edit'
>
> I tried matching the pattern-to-remove with gsub and substituting an
> empty string, but that leaves me with lots of blank lines and not
> really any nearer to the answer.


You could change your lines into an array of lines and then remove the
lines that match:

lines =3D []
File.new("text.txt").read.each_line {|line| lines << line }
lines.delete_if {|line| line =3D~ /\/usr\/local\/lib/}




--=20
If you could create a machine that copies hamburgers =97 you put one
hamburger in and two equally good hamburgers come out the other side =97
it would be unethical not to do so and make it freely available.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      03-22-2007

On Mar 22, 2007, at 10:43 AM, Andrew Stewart wrote:

> Hello,
>
> What's a (good!) way to remove lines matching a pattern from a
> multiline string?
>
> For example, I would like to remove lines matching /usr/local/lib
> from the multiline string:
>
> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
> action_controller/test_process.rb:382:in `process'
> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
> action_controller/test_process.rb:353:in `post'
> test/functional/orders_controller_test.rb:241:in
> `test_should_handle_errors_on_edit'
>
> ...to give:
>
> test/functional/orders_controller_test.rb:241:in
> `test_should_handle_errors_on_edit'
>
> I tried matching the pattern-to-remove with gsub and substituting
> an empty string, but that leaves me with lots of blank lines and
> not really any nearer to the answer.
>
> Thanks and regards,
> Andy Stewart


>> input = " /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/

lib/action_controller/test_process.rb:382:in `process'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/test_process.rb:353:in `post'
test/functional/orders_controller_test.rb:241:in
`test_should_handle_errors_on_edit'
"
=> " /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/test_process.rb:382:in `process'\n /usr/local/
lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/
test_process.rb:353:in `post'\n test/functional/
orders_controller_test.rb:241:in `test_should_handle_errors_on_edit'\n"

>> input.gsub(%r{^.*/usr/local/lib/.*\n?},'')

=> " test/functional/orders_controller_test.rb:241:in
`test_should_handle_errors_on_edit'\n"


If you showed your code, an explanation could be added as to your
regexp, but the concept certainly works as I've shown.

-Rob


Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      03-22-2007
On Mar 22, 9:15 am, "Leslie Viljoen" <(E-Mail Removed)> wrote:
> You could change your lines into an array of lines and then remove the
> lines that match:
>
> lines = []
> File.new("text.txt").read.each_line {|line| lines << line }
> lines.delete_if {|line| line =~ /\/usr\/local\/lib/}


Leslie, as a public service announcement, you should be aware of
IO.readlines:

C:\>qri IO.readlines
----------------------------------------------------------
IO::readlines
IO.readlines(name, sep_string=$/) => array
------------------------------------------------------------------------
Reads the entire file specified by _name_ as individual lines,
and
returns those lines in an array. Lines are separated by
_sep_string_.

a = IO.readlines("testfile")
a[0] #=> "This is line one\n"


For that matter, you should also be aware of IO.read:
---------------------------------------------------------------
IO::read
IO.read(name, [length [, offset]] ) => string
------------------------------------------------------------------------
Opens the file, optionally seeks to the given offset, then
returns
_length_ bytes (defaulting to the rest of the file). +read+
ensures
the file is closed before returning.

IO.read("testfile") #=> "This is line one\nThis is
line two\nThis is line three\nAnd so on...\n"
IO.read("testfile", 20) #=> "This is line one\nThi"
IO.read("testfile", 20, 10) #=> "ne one\nThis is line "

You should also be aware of the block form of #open, which opens the
IO object and then closes it when done.

What you wrote creates a new File object and opens it, but never
closes it. I'm not really sure what badness can result from this, but
I gather it's not a good idea.


 
Reply With Quote
 
Andrew Stewart
Guest
Posts: n/a
 
      03-22-2007

On 22 Mar 2007, at 15:15, Robert Klemme wrote:
> >> "foo\nbar\n".select {|l| /^f/ =~ l}

> => ["foo\n"]


Thanks for that. So simple once you've seen it.

Regards,
Andy Stewart

 
Reply With Quote
 
Andrew Stewart
Guest
Posts: n/a
 
      03-22-2007

On 22 Mar 2007, at 15:15, Leslie Viljoen wrote:
> lines.delete_if {|line| line =~ /\/usr\/local\/lib/}


Leslie, thanks for that. That works for me (with a join chained on
the end).

Regards,
Andy Stewart


 
Reply With Quote
 
Andrew Stewart
Guest
Posts: n/a
 
      03-22-2007

On 22 Mar 2007, at 15:22, Rob Biedenharn wrote:
> >> input.gsub(%r{^.*/usr/local/lib/.*\n?},'')

>
> If you showed your code, an explanation could be added as to your
> regexp, but the concept certainly works as I've shown.


Aha! You have proved that I chose my regexp poorly. Here's what I
tried:

input.gsub(%r{^.*/usr/local/lib/.*$}i, '')

The difference is that yours consumes the new line character but mine
doesn't. I should have just matched it explicitly like you rather
than using an anchor.

Thanks and regards,
Andy Stewart

 
Reply With Quote
 
Leslie Viljoen
Guest
Posts: n/a
 
      03-22-2007
On 3/22/07, Phrogz <(E-Mail Removed)> wrote:
> On Mar 22, 9:15 am, "Leslie Viljoen" <(E-Mail Removed)> wrote:
> > You could change your lines into an array of lines and then remove the
> > lines that match:
> >
> > lines =3D []
> > File.new("text.txt").read.each_line {|line| lines << line }
> > lines.delete_if {|line| line =3D~ /\/usr\/local\/lib/}

>
> Leslie, as a public service announcement, you should be aware of
> IO.readlines:
>
> C:\>qri IO.readlines
> ----------------------------------------------------------
> IO::readlines
> IO.readlines(name, sep_string=3D$/) =3D> array
> ------------------------------------------------------------------------
> Reads the entire file specified by _name_ as individual lines,
> and
> returns those lines in an array. Lines are separated by
> _sep_string_.
>
> a =3D IO.readlines("testfile")
> a[0] #=3D> "This is line one\n"
>
>
> For that matter, you should also be aware of IO.read:
> ---------------------------------------------------------------
> IO::read
> IO.read(name, [length [, offset]] ) =3D> string
> ------------------------------------------------------------------------
> Opens the file, optionally seeks to the given offset, then
> returns
> _length_ bytes (defaulting to the rest of the file). +read+
> ensures
> the file is closed before returning.
>
> IO.read("testfile") #=3D> "This is line one\nThis is
> line two\nThis is line three\nAnd so on...\n"
> IO.read("testfile", 20) #=3D> "This is line one\nThi"
> IO.read("testfile", 20, 10) #=3D> "ne one\nThis is line "
>
> You should also be aware of the block form of #open, which opens the
> IO object and then closes it when done.
>
> What you wrote creates a new File object and opens it, but never
> closes it. I'm not really sure what badness can result from this, but
> I gather it's not a good idea.


This does sound rather frightening! What *is* the effect of opening a
file and not closing it?


Also, doesn't the above say that IO.read closes the file afterwards?


--=20
If you could create a machine that copies hamburgers =97 you put one
hamburger in and two equally good hamburgers come out the other side =97
it would be unethical not to do so and make it freely available.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Knocking The Bottom Out With The D3!! Rita Berkowitz Digital Photography 2 03-03-2008 02:38 PM
HELP! Master Browser elections are knocking out my wireless connections Jeff Wireless Networking 2 10-12-2007 09:52 PM
Opportunity Is Knocking Freecomputergetter. DVD Video 2 12-12-2004 03:55 PM
how to define a variable to hold a multiline text input in perl from html multiline textbox dale zhang Perl Misc 8 11-30-2004 06:53 AM
RIAA knocking on evil doer p-2-p uploaders doors to search-n-seize......... 'Ole Computer Security 16 02-22-2004 03:34 AM



Advertisments