[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

An Array of grep Results?

Nathan O.

3/10/2006 10:32:00 PM

I want to search all the files in a directory for a string and save the
results in an Array. So far, the best solution I can come up with is
using system("grep ... > resultsFile"), then opening resultsFile and
doing readlines(). There's a better way, isn't there?

--
Posted via http://www.ruby-....


2 Answers

Jonathan Leighton

3/10/2006 10:51:00 PM

0

On Sat, 2006-03-11 at 07:32 +0900, Nathan Olberding wrote:
> I want to search all the files in a directory for a string and save the
> results in an Array. So far, the best solution I can come up with is
> using system("grep ... > resultsFile"), then opening resultsFile and
> doing readlines(). There's a better way, isn't there?

Yep: Dir["/some/foo/directory/*"] ;)

See the docs for Dir#glob

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...



Robert Klemme

3/11/2006 12:55:00 PM

0

Nathan Olberding <nathan.olberding@gmail.com> wrote:
> I want to search all the files in a directory for a string and save
> the results in an Array. So far, the best solution I can come up with
> is using system("grep ... > resultsFile"), then opening resultsFile
> and doing readlines(). There's a better way, isn't there?

# untested
found = {}
Dir["/foo/*"].each {|f| found[f] = File.open(f) {|io|
io.grep(/your_string/)} }

This should give you a hash with file names as keys and match lines as
values.

Kind regards

robert