Newbie help: File searching and writing output to new file

Your problem in find_redir that you were opening the file for write for
each of the input files. When you open the file for write, it zero's out
the file. I'm guessing that the last file that you open doesn't have any
matches.

This will do the what you want:

def find_redir
  File.open("found-redir-lines.txt", "w") do |out|
    Dir['ex*.log'].each do |in|
      in.grep(/redir/) { |line| out.puts(line) }
    end
  end
end

That way, you're also not keeping all the lines in memory - you're just
outputting each one as you find it.

···

--------------------
#!c:\ruby\bin\ruby

def find_redir
  found =
  Dir['ex*.log'].each do |file|
    file.each_line do |line|
      #found.push line if line =~ /redir/
      found.push line if line["redir"]
      #puts line if line["redir"]
    end
    #p found.join
    File.open("found-redir-lines.txt", "w+") do |o|
      o.write found.join
    end
  end
end

def show_500_errors
  f = File.open("found-redir-lines.txt", "r")
  f.each do |line|
    puts line if line["500"]
  end
end

find_redir()
#show_500_errors()
-------------------

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################