/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Simplest fgrep 'mystring' filename.txt Now it depends on whether to just verify the string is there: def check_file( file, string ) File.open( file ) do |io| io.each {|line| line.chomp! ; return true if line.include? string} end false end or whether to get the first matching line: def check_file( file, string ) File.open( file ) do |io| io.each {|line| line.chomp! ; return line if line.include? string} end nil end or whether to get all matching lines: def check_file( file, string ) lines=[] File.open( file ) do |io| io.each {|line| line.chomp! ; lines << line if line.include? string} end lines end or whether the condition is a regexp: def check_file( file, rx ) File.open( file ) do |io| io.each {|line| line.chomp! ; return true if rx =~ line} end false end