From: Chris White Date: 2011-08-20T04:42:10+09:00 Subject: Re: Searching in a directory On Aug 19, 2011, at 12:27 PM, Yu Yu wrote: > dir_path = 'c:/temp' > str = "string" > > Dir.chdir(dir_path) do > > Dir.glob("*.txt") do |filename| > IO.foreach(filename) do |line| > if line =~ /#{str}/ > puts "#{filename}: #{str}" > end > end > end > end I'm not sure what different action you plan to take if there is a double instance but: Dir.glob("test.txt") do |filename| IO.foreach(filename).inject(nil) do |last_line, line| if line =~ /#{str}/ puts "#{filename}: #{str}" if last_line == line puts "Double line" end end line end end This basically uses inject to keep a temporary holder of the last line, so you can check if it matches the previous one and take a different course of action for that case. The nil is passed as the initial value to inject so it won't match if the first string is blank. Regards, Chris White http://www.twitter.com/cwgem