Just to be said. I am new to ruby.

This should iterate over an array of files, read them and match the
lines by a given pattern. And print to console, But how to proceed if i
want all of the matched lines to be written to a new file?




$files = Array.new

Dir.glob("**/Msg.log").entries.each { |n| $files.push(n) if
File.file?(n) }
puts "\n#{$files.size} files found\n"

pattern = /(failed)/m

$files.each do |element|
         f = File.open(element, 'r')
              lines = f.readlines
              lines.each do |line|
                  if line.match(pattern)
                      puts line
                        end
                     end
                     f.close
                end



I have tried different solutions as
File.open('newfile.txt', 'w') do |wirte|
write.puts(line)

but it did not work

Any suggestions? Thanks

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