[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Found a neat trick for doing recursive one-liners

Pat Maddox

12/27/2005 4:06:00 AM

Or you can use the tools designed for finding stuff :)

find . -name "*.txt" | xargs grep Hello

That version will work for all files. You can play with find to match
any file you want.

Pat


On 12/26/05, Gary Watson <pfharlock@yahoo.com> wrote:
> 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.
>
>


10 Answers

Gary Watson

12/27/2005 4:42:00 AM

0

I apologize for using a brain dead example. I was more excited about the
prospect of hitting all files under the current directory recursively, not
the actual processing I used in my examples. Thanks for the pointer to
xargs. I didn't know about that one, I'll have to take a closer look at
it's man page.

On Tue, 27 Dec 2005 13:05:32 +0900, Pat Maddox wrote:

> Or you can use the tools designed for finding stuff :)
>
> find . -name "*.txt" | xargs grep Hello
>
> That version will work for all files. You can play with find to match
> any file you want.
>
> Pat


Gary Allum

12/27/2005 4:52:00 AM

0

I know Im a n00b, but I think more than anything, its good to see you so
excited about Ruby. Learning new things really is fun in Ruby.

On Mon, 26 Dec 2005 20:22:52 -0800, Gary Watson <pfharlock@yahoo.com>
wrote:

> I apologize for using a brain dead example. I was more excited about the
> prospect of hitting all files under the current directory recursively,
> not
> the actual processing I used in my examples. Thanks for the pointer to
> xargs. I didn't know about that one, I'll have to take a closer look at
> it's man page.
>
> On Tue, 27 Dec 2005 13:05:32 +0900, Pat Maddox wrote:
>
>> Or you can use the tools designed for finding stuff :)
>>
>> find . -name "*.txt" | xargs grep Hello
>>
>> That version will work for all files. You can play with find to match
>> any file you want.
>>
>> Pat
>
>
>



--
Using Opera's revolutionary e-mail client: http://www.opera...


james_b

12/27/2005 5:15:00 AM

0

Pat Maddox wrote:
> Or you can use the tools designed for finding stuff :)
>
> find . -name "*.txt" | xargs grep Hello
>
> That version will work for all files. You can play with find to match
> any file you want.

Assuming you are on a machine with find, xargs, and grep, as opposed to
just Ruby.

I like the idea of assembling command line utils that will work on any
platform where Ruby is installed (e.g., all the machines in my house).

I also like the idea of reinventing the wheel in Ruby because sometimes
you get a better wheel. Or at least one that is more hackable.

James

--

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - Ruby Code & Style: Writers wanted
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


Reinder Verlinde

12/27/2005 10:53:00 AM

0

In article
<810a540e0512262005s3e4c971bl2f2915954c34965f@mail.gmail.com>,
Pat Maddox <pergesu@gmail.com> wrote:

> Or you can use the tools designed for finding stuff :)
>
> find . -name "*.txt" | xargs grep Hello
>
> That version will work for all files.

<educational type="but not ruby related">
Not quite. For better (maximum?) robustness, pass '-print0' to find and
'-0' to xargs. That will handle filenames with spaces and/or quotes
correctly. (If your filenames have bytes with binary value zero in them,
you still will be out of luck)
</educational>

Reinder

Stefan Walk

12/27/2005 2:45:00 PM

0

Pat Maddox wrote:
> Or you can use the tools designed for finding stuff :)
>
> find . -name "*.txt" | xargs grep Hello
>
> That version will work for all files. You can play with find to match
> any file you want.
>
> Pat
>

et@adel:/tmp/rb$ touch 'foo bar.txt'
et@adel:/tmp/rb$ find . -name "*.txt"
../foo bar.txt
et@adel:/tmp/rb$ find . -name "*.txt" | xargs grep Hello
grep: ./foo: No such file or directory
grep: bar.txt: No such file or directory
et@adel:/tmp/rb$ find . -name "*.txt" -print0 | xargs -0 grep Hello
et@adel:/tmp/rb$

Watch out if you are using xargs. It can get pretty nasty, especially if
there is not grep at work, but rm or alike.

Shooting yourself in the foot 101:
$ touch "foo .. bar -rf moo.o"
$ find . -name '*.o' | xargs rm
*BAM*

If you are using find and xargs, always use -print0 and -0, respectively.

Regards,
Stefan

W.B.Hill

12/27/2005 2:47:00 PM

0

Christian Neukirchen

12/27/2005 3:31:00 PM

0

Reinder Verlinde <reinder@verlinde.invalid> writes:

> <educational type="but not ruby related">
> Not quite. For better (maximum?) robustness, pass '-print0' to find and
> '-0' to xargs. That will handle filenames with spaces and/or quotes
> correctly. (If your filenames have bytes with binary value zero in them,
> you still will be out of luck)
> </educational>

Know an OS where that is allowed?

> Reinder
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


W.B.Hill

12/27/2005 3:41:00 PM

0

Christian Neukirchen

12/27/2005 6:03:00 PM

0

"W.B.Hill" <mail@wbh.org> writes:

> On Wed, 28 Dec 2005, Christian Neukirchen wrote:
>
>> Reinder Verlinde <reinder@verlinde.invalid> writes:
>>
>>> <educational type="but not ruby related">
>>> Not quite. For better (maximum?) robustness, pass '-print0' to find and
>>> '-0' to xargs. That will handle filenames with spaces and/or quotes
>>> correctly. (If your filenames have bytes with binary value zero in them,
>>> you still will be out of luck)
>>> </educational>
>>
>> Know an OS where that is allowed?
>
> NT 4 kernel mode API is quite happy with \0 in filenames. But it
> *really* confuses the Win32 layer! Haven't played with later
> versions...

So you need to pass the filename size all over? Ugh. :P

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Daniel Calvelo

12/27/2005 8:59:00 PM

0

Great!

Would any mad enough rubyists start the equivalent of
http://ppt.... For the impatient, "perl power tools: unix
reconstruction project" is an on-going attempt at writing (most of) the
BSD command set in Perl.

Not quite as mad as BASIC in TeX, but potentially useful indeed.

--DCA