From: 7stud -- Date: 2009-10-13T02:50:03+09:00 Subject: Re: find a print out a line Collin Moore wrote: > how would i write the output to a text file? I tried the following, but > the output displays blank. > > I tried a couple File.open attempts, but the output is blank. fout = File.open("data2.txt", "w") File.open("data.txt") do |f| f.each_line do |line| if line.include?("TestPhrase") fout.puts(line) end end end fout.close ...or File.open("results.txt", "w") do |fout| IO.foreach("data.txt") do |line| if line.include?("TestPhrase") fout.puts line end end end If you use blocks with File.open(), then you don't need to manually close() the file--the block will do it automatically. -- Posted via http://www.ruby-forum.com/.