Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Extract lines with regular expressions
Michael Dore
4/21/2008 5:49:00 AM
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/rub...
)
# 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-t...
)
But I am not sure how to seal the deal, as it were...
Any thoughts would be appreciated.
Thanks!
--
Posted via
http://www.ruby-...
.
2 Answers
Robert Klemme
4/21/2008 6:29:00 AM
0
On 21.04.2008 07:49, Michael Dore wrote:
> 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/rub...
)
>
> # 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-t...
)
>
> But I am not sure how to seal the deal, as it were...
> Any thoughts would be appreciated.
> Thanks!
files.each do |file|
File.open(directory+file) do |f|
puts f.grep(search)
end
Or, for large files
files.each do |file|
File.open(directory+file) do |f|
f.each do |line|
puts line if search ~= line
end
end
Kind regards
robert
7stud --
4/21/2008 8:55:00 AM
0
Michael Dore wrote:
>
> I have tried this:
>
> vl_stops.each do |vl_stops|
First, you might try dreaming up more than one variable name.
Second, if you are searching for simple strings, like 'hello', you can
do this:
search_string = 'hello'
IO.foreach("data.txt") do |line|
if line.include?(search_string)
puts line
end
end
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Extract lines with regular expressions
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password