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.
At Tue, 27 Dec 2005 12:57:53 +0900,
Gary W. 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’
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.
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'
ruby -e’ puts Dir[“/”].select{|e| e =~ /a.rb/} ’
We’ve strayed a little from the original thing, but along those lines
you could also do:
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'
ruby -e’ puts Dir["/"].select{|e| e =~ /a.rb/} ’
That doesn’t seem to do anything . .
Phil.
Philip R.
Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: [email protected]
Or you can use the tools designed for finding stuff
find . -name “*.txt” | xargs grep Hello
That version will work for all files.
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)
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)
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…
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)