Search String and Delete Line Containing String

B

Bob Hatch

I am new to Ruby and am pulling my hair out on something that I know is
simple. I am searching a large text file, line by line for text that
will reside in part of the line. If it finds part of the text, I want to
delete the line. Here is what I have that is not working. Help is
appreciated! Thanks in advance!

# process every line in a text file
file='c:\ruby192\my_projects\IIS_Logs\ex11012607.log'
text=File.readlines(file).each do |line|
search_text = "/memberinfo/downline/tree/canvas.asp"
if(line.gsub!(search_text) == search_text) then
#line.clear
puts search_text
else
puts "Not Found"
end
#puts line
end
 
B

Bob Hatch

I am new to Ruby and am pulling my hair out on something that I know is
simple. I am searching a large text file, line by line for text that
will reside in part of the line. If it finds part of the text, I want to
delete the line. Here is what I have that is not working. Help is
appreciated! Thanks in advance!

# process every line in a text file
file='c:\ruby192\my_projects\IIS_Logs\ex11012607.log'
text=File.readlines(file).each do |line|
search_text = "/memberinfo/downline/tree/canvas.asp"
if(line.gsub!(search_text) == search_text) then
#line.clear
puts search_text
else
puts "Not Found"
end
#puts line
end
 
J

Jeremy Bopp

I am new to Ruby and am pulling my hair out on something that I know is
simple. I am searching a large text file, line by line for text that
will reside in part of the line. If it finds part of the text, I want to
delete the line. Here is what I have that is not working. Help is
appreciated! Thanks in advance!

# process every line in a text file
file='c:\ruby192\my_projects\IIS_Logs\ex11012607.log'
text=File.readlines(file).each do |line|
search_text = "/memberinfo/downline/tree/canvas.asp"
if(line.gsub!(search_text) == search_text) then
#line.clear
puts search_text
else
puts "Not Found"
end
#puts line
end

path='c:\ruby192\my_projects\IIS_Logs\ex11012607.log'
search_text = %r{/memberinfo/downline/tree/canvas.asp}
File.open(path) do |file|
file.each_line do |line|
puts(line) unless line =~ search_text
end
end

-Jeremy
 
B

Brian Candler

Bob Hatch wrote in post #978955:
I am new to Ruby and am pulling my hair out on something that I know is
simple. I am searching a large text file, line by line for text that
will reside in part of the line. If it finds part of the text, I want to
delete the line. Here is what I have that is not working. Help is
appreciated! Thanks in advance!

# process every line in a text file
file='c:\ruby192\my_projects\IIS_Logs\ex11012607.log'
text=File.readlines(file).each do |line|
search_text = "/memberinfo/downline/tree/canvas.asp"
if(line.gsub!(search_text) == search_text) then

gsub! returns the whole line after the substitution has taken place, or
nil if no substitution has taken place. So you should test for nil/not
nil, not compare against the substitution text.

irb is the way to find this sort of thing out:

$ irb --simple-prompt=> nil

Also, gsub! should take two arguments as I've shown. But rather than
raise an error, the joys of ruby 1.9/1.8.7 mean that it does something
unexpected when you give it one argument:
=> #<Enumerable::Enumerator:0x7f517774d5b8>

It returns an Enumerator object. In this case, I'd say that's pretty
confusing behaviour, and very rarely useful. It's because there is a
one-argument form of gsub which takes a block:
=> "3 xxx baz"

And if you omit the block, it's returning an enumerator for consistency
with each/map/select, which return an enumerator when invoked without a
block (also not particularly useful IMO).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top