Ask a Question related to Ruby, Design and Development.
-
Chad Fowler #1
Re: Search string in a file
On Wed, 15 Oct 2003, Panther wrote:
# I must search string in a file ??
# How I must open file in read and search string ??
# What is syntax ??
# Thamk you
#
ruby -ne 'print if /mystring/' filename.txt
Chad Fowler Guest
-
search a string for value
hi, i have a field that has descriptions for a product.. i want to do a search on this field.. but i'm getting bad results because some users... -
String Search with SQL
I was trying to write an SQL statment that will do a string search in an Access 2000 database. But I can't seem to figure out how to do it so that... -
how to optimize a string search
I know this is more of an algorithm question but please bear with me. In my program I am checking wether a emailid exists in a list I have in... -
String question: Returning portion of string with words surrounding highlighted search term?
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string,... -
string search
Is there a way to search a string within a string. For example, I want to search for "world" in "Hello world" knowing that "world" can be anywhere... -
Robert Klemme #2
Re: Search string in a file
"Chad Fowler" <chad@chadfowler.com> schrieb im Newsbeitrag
news:Pine.LNX.4.44.0310150841280.26441-100000@ns1.chadfowler.com...I guess the OP is searching for a ruby solution, that can be embedded in a> On Wed, 15 Oct 2003, Panther wrote:
>
> # I must search string in a file ??
> # How I must open file in read and search string ??
> # What is syntax ??
> # Thamk you
> #
>
> ruby -ne 'print if /mystring/' filename.txt
script. Otherwise this is likely to be simpler and more efficient:
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
etc.
Cheers
robert
Robert Klemme Guest
-
Jason Williams #3
Re: Search string in a file
In article <bmjh13$nopuc$1@ID-52924.news.uni-berlin.de>, Robert Klemme wrote:
How about>>> ruby -ne 'print if /mystring/' filename.txt
> I guess the OP is searching for a ruby solution, that can be embedded in a
> script. Otherwise this is likely to be simpler and more efficient:
>
> fgrep 'mystring' filename.txt
>
> [...snip...]
file.grep(/mystring/) { |s| puts s }
?
Jason Williams Guest
-
Robert Klemme #4
Re: Search string in a file
"Jason Williams" <jason@jasonandali.org.uk> schrieb im Newsbeitrag
news:slrnboqige.dt.jason@kotu.jasonandalishouse.or g.uk...wrote:> In article <bmjh13$nopuc$1@ID-52924.news.uni-berlin.de>, Robert Klemmein a> >> >> ruby -ne 'print if /mystring/' filename.txt
> > I guess the OP is searching for a ruby solution, that can be embeddedI guess you mean:>> > script. Otherwise this is likely to be simpler and more efficient:
> >
> > fgrep 'mystring' filename.txt
> >
> > [...snip...]
> How about
>
> file.grep(/mystring/) { |s| puts s }
File.open( file ) {|io| io.grep(/mystring/) { |s| puts s }}
or
File.open( file ) {|io| io.grep(/mystring/)}
That doesn't get rid of line terminations. One can do
File.open( file ) {|io| io.grep(/mystring/).each{|line|line.chomp!}}
Yes, that's nicer if one does not want the simple check / first line. In
those cases it's more efficient for large files to short circuit at the
first match.
Cheers
robert
Robert Klemme Guest
-
Josef 'Jupp' SCHUGT #5
Re: Search string in a file
Hi!
* Panther; 2003-10-15, 12:00 UTC:Two are possible. The following reads file 'groups' as a whole and> I must search string in a file ??
> How I must open file in read and search string ??
> What is syntax ??
then prints out all lines that match /anime/ Regexp:
File.readlines("groups").each{ |line|
print line if line =~ /anime/
}
You can alternatively open "groups" and repeat the following until
EOF has been reached: Read in one line and print it if it matches
/anime/ Regexp.
File.open("groups") { |file|
while file.gets
print $_ if /anime/
end
}
HTH
Please take notice of signature! / Bitte Signature beachten!
Josef 'Jupp' Schugt
--
db Wenn sie mir ohne meine Einwilligung geschickt wurde, db
dpqb wird eine E-Mail > 100 kB ungelesen entsorgt dpqb
dp::qb If you send me an e-mail > 100 kB without my dp::qb
dp::::qb consent it will be silently discarded dp::::qb
Josef 'Jupp' SCHUGT Guest
-
Rob Partington #6
Re: Search string in a file (tangent)
On Thu, 16 Oct 2003 06:54:07 +0900,
Josef 'Jupp' SCHUGT <jupp@gmx.de> wrote:^^^^^^^^^^^^^^^^^^^^^^^^> File.readlines("groups").each{ |line|
It just struck me that this is horrible. It would be far nicer as
File("groups").readlines.each{ |line|
since then the filename is associated with the File part, not detached
and attached to some random method. Same goes for opening a file.
File("groups").open { |file|
Perhaps I'm crazy, but that reads far more naturally to me ("create
a file object on this file, then do something to it".)
I might even submit that as an RCR since it's so lovely.
Rob Partington Guest
-
Robert Klemme #7
Re: Search string in a file (tangent)
"Rob Partington" <rjp@frottage.org> schrieb im Newsbeitrag
news:slrnbp9sac.hid.rjp@ipy.frottage.org...Hmmm, I don't like that. File is a class and open is a class method. So> On Thu, 16 Oct 2003 06:54:07 +0900,
> Josef 'Jupp' SCHUGT <jupp@gmx.de> wrote:> ^^^^^^^^^^^^^^^^^^^^^^^^> > File.readlines("groups").each{ |line|
> It just struck me that this is horrible. It would be far nicer as
>
> File("groups").readlines.each{ |line|
>
> since then the filename is associated with the File part, not detached
> and attached to some random method. Same goes for opening a file.
>
> File("groups").open { |file|
File.open(name) is perfectly ok for me.
You can do:> Perhaps I'm crazy, but that reads far more naturally to me ("create
> a file object on this file, then do something to it".)
>
> I might even submit that as an RCR since it's so lovely.
class FileProxy
def initialize(fileName)
@fileName = fileName
end
def method_missing(sym, *args)
File.open(@fileName) do |io|
io.send( sym, *args )
end
end
def open(mode="r")
File.open(@fileName, mode)
end
end
module Kernel
def File(name)
FileProxy.new(name)
end
end
now you can do
File("groups").readlines.each{ |line| ... }
File("groups").open.readlines.each{|line| ... }
But personally I don't like this construction since it requires a
temporary instance that does nothing else but stores the file name for
later use. And you don't really gain much readbility...
Another thing I'd like to mention is that for large files
File.readlines().each might be awfully imperformant since the whole file
has to be sucked into mem. In those cases I prefer the block form anyway:
File.open("groups"){|io| io.each {|line| ...} }
Regards
robert
Robert Klemme Guest
-
gabriele renzi #8
Re: Search string in a file (tangent)
il 21 Oct 2003 08:41:48 GMT, Rob Partington <rjp@frottage.org> ha
scritto::
I think this is really subjective. But I would not understand the 2nd>On Thu, 16 Oct 2003 06:54:07 +0900,
>Josef 'Jupp' SCHUGT <jupp@gmx.de> wrote:> ^^^^^^^^^^^^^^^^^^^^^^^^>> File.readlines("groups").each{ |line|
>It just struck me that this is horrible. It would be far nicer as
>
> File("groups").readlines.each{ |line|
>
>since then the filename is associated with the File part, not detached
>and attached to some random method. Same goes for opening a file.
>
> File("groups").open { |file|
>
>Perhaps I'm crazy, but that reads far more naturally to me ("create
>a file object on this file, then do something to it".)
choice easily.
gabriele renzi Guest



Reply With Quote

