From: Leslie Viljoen Date: 2007-03-23T00:15:45+09:00 Subject: Re: Knocking Lines Out Of A Multiline String On 3/22/07, 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. 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/} -- If you could create a machine that copies hamburgers � you put one hamburger in and two equally good hamburgers come out the other side � it would be unethical not to do so and make it freely available.