[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Found a neat trick for doing recursive one-liners

Gary Watson

12/27/2005 4:18:00 AM

This is probably something everyone in here already knows about, but I
thought it was cool enough that I wanted to post about it.

If you want to create a one liner to say search all the *.txt files
in and under the current directory for text matching "Hello", you can do
this

find -name '*.txt' -exec ruby -ne 'print if /Hello/' '{}' ';'

I know you can do this in pure ruby in like 3 lines if you use the Find
module, but I really wanted to do it with a one liner. Earlier I tried
something like this

ruby -ne 'print if /Hello/' `find -name '*.txt'`

unfortunately that version would fail if there were any spaces in the
filenames.
1 Answer

Nakada, Nobuyoshi

12/27/2005 4:21:00 AM

0

Hi,

At Tue, 27 Dec 2005 12:57:53 +0900,
Gary Watson wrote in [ruby-talk:172611]:
> I know you can do this in pure ruby in like 3 lines if you use the Find
> module, but I really wanted to do it with a one liner. Earlier I tried
> something like this
>
> ruby -ne 'print if /Hello/' `find -name '*.txt'`
>
> unfortunately that version would fail if there were any spaces in the
> filenames.

ruby -ne 'BEGIN{ARGV.replace(Dir[ARGV.join("\0")])}; print if /Hello/' '**/*.txt'

--
Nobu Nakada