[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How do I use grep with the Shell class

Mark Wilson

9/17/2003 5:14:00 AM

Hello,

I'm trying to do the following in Ruby using the Shell class:

From the command line, I would write this:

% grep -l PATTERN * | xargs mv --target-directory=DIRECTORY

Because I have many directories I want to visit in turn, I've tried the
following:

directories.each do |d|
shell = Shell.cd(d)
string = String.new
shell.transact do
string = shell.grep("-l", "PATTERN", "*") # => the '*' is not
expanded;
#
grep: *: No such file or directory
end
array = string.split
array.each { |e| File.move(e, "DIRECTORY") }
end

The above does not work. Does anyone have any insight (or alternate
ways to accomplish the same purpose in Ruby)?

Thank you.

Regards,

Mark


2 Answers

Robert Klemme

9/17/2003 9:21:00 AM

0


"Mark Wilson" <mwilson13@cox.net> schrieb im Newsbeitrag
news:B991E5D0-E8CD-11D7-85D0-000393876156@cox.net...
> Hello,
>
> I''m trying to do the following in Ruby using the Shell class:
>
> From the command line, I would write this:
>
> % grep -l PATTERN * | xargs mv --target-directory=DIRECTORY
>
> Because I have many directories I want to visit in turn, I''ve tried the
> following:
>
> directories.each do |d|
> shell = Shell.cd(d)
> string = String.new
> shell.transact do
> string = shell.grep("-l", "PATTERN", "*") # => the ''*'' is not
> expanded;
> #
> grep: *: No such file or directory
> end
> array = string.split
> array.each { |e| File.move(e, "DIRECTORY") }
> end
>
> The above does not work. Does anyone have any insight (or alternate
> ways to accomplish the same purpose in Ruby)?

untested:

def fileGrep(file, pattern)
File.open(file) do |f|
f.each_line do |line|
line.chomp!
# exit as soon as possible
return true if pattern =~ line
end
end

false
end

directories.each do |d|
Dir.foreach( d ) do |file|
File.rename( File.join( d, file ), File.join( TARGET_DIR, file ) ) if
fileGrep( file, /PATTERN/ )
end
end

Regards

robert

Jason Creighton

9/17/2003 9:29:00 PM

0

On Wed, 17 Sep 2003 14:13:57 +0900
Mark Wilson <mwilson13@cox.net> wrote:

> Hello,
>
> I''m trying to do the following in Ruby using the Shell class:
>
> From the command line, I would write this:
>
> % grep -l PATTERN * | xargs mv --target-directory=DIRECTORY
>
> Because I have many directories I want to visit in turn, I''ve tried the
> following:
>
> directories.each do |d|
> shell = Shell.cd(d)
> string = String.new
> shell.transact do
> string = shell.grep("-l", "PATTERN", "*") # => the ''*'' is not
> expanded;
> #
> grep: *: No such file or directory
> end
> array = string.split
> array.each { |e| File.move(e, "DIRECTORY") }
> end
>
> The above does not work. Does anyone have any insight (or alternate
> ways to accomplish the same purpose in Ruby)?

Something like this, perhaps?

directories.each do |d|
Dir["#{d}/*"].each do |filename|
f = File.open(filename)
if f.detect { |line| line =~ /PATTERN/ }
File.rename(filename, File.join("DIRECTORY", File.basename(filename)))
end
f.close()
end
end

Using the command line would probably be easier for a quick job like
this, however. (Unless you really need to do this from within Ruby)

Jason Creighton