7stud -- wrote:
>> Anyone know a good way to do this?
> 
> prev_lines = []
> 
> IO.foreach("data.txt") do |line|
>   if line =~ /good/
>     prev_lines.reverse_each do |prev_line|
>       if prev_line =~ /hel/
>         puts prev_line
>         prev_lines = []
>       end
>     end
>   end
> 
>   prev_lines << line
> end

Of course, if you're files aren't really big (e.g. 1+ GB), you can 
simply read the whole file into memory:

lines = File.readlines("data.txt")

lines.each do |line|
  if line =~ /good/
    lines.reverse_each do |prev_line|
      if prev_line =~ /hel/
        puts prev_line
      end
    end
  end
end


-- 
Posted via http://www.ruby-forum.com/.