From: Robert Hansen Date: 2012-06-04T23:01:33+09:00 Subject: Re: Read from several .txt files by a pattern and write the matched lines to a new file Robert Hansen wrote in post #1063024: > $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) > File.open('newfile.txt', 'w') do |write| <-- Problem > write.puts(line) > end > end > end > f.close > end > > My problem is where the "if statement" begins. I am sorry to say that my > compiler does not give me any error message, but the "newfile.txt" will > not be created and therefore not written to. This makes no sense to me > since I am a new beginner at Programming/scripting > > As for your code, > > "$ find . -type f -name Msg.log -exec fgrep failed {} + >newfile.txt" > > looks pretty and neat, but I am afraid it is way over my head. > > Kind regard, Robert Hansen Somewhere in the script i changed the working directory, so it was my mistake, but feel free to suggest better code. Everything will be appreciated as I am still learning. Kind regard Robert Hansen -- Posted via http://www.ruby-forum.com/.