From: Michael Dore Date: 2008-04-21T14:49:20+09:00 Subject: Extract lines with regular expressions I am a newb trying to take a list of data in a text file and print out a new list of lines that contain certain characters... I am not too worried about the actual regex - I can figure that out - but I cannot seem to get it to implement correctly. I found the following online (http://www.fepus.net/ruby1line.txt) # print only lines that match a regular expression (emulates 'grep') $ ruby -pe 'next unless $_ =~ /regexp/' < file.txt and it does exactly what I want, but... how do I use this (or something similar, ie grep) in a .rb script? I have tried this: vl_stops.each do |vl_stops| puts "#{vl_stops}" search = Regexp.new("^\b(#{vl_stops}:)\w*") files.each do |file| f = File.open(directory+file) f.grep(search) puts #what goes here? line? end f.close end puts end based on something I found here (http://www.rousette.org.uk/blog/archives/2004/10/21/gtd-using-text-files/) But I am not sure how to seal the deal, as it were... Any thoughts would be appreciated. Thanks! -- Posted via http://www.ruby-forum.com/.