Professional Web Applications Themes

How do I use grep with the Shell class - Ruby

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 ...

  1. #1

    Default How do I use grep with the Shell class

    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


    Mark Wilson Guest

  2. #2

    Default Re: How do I use grep with the Shell class


    "Mark Wilson" <mwilson13cox.net> schrieb im Newsbeitrag
    news:B991E5D0-E8CD-11D7-85D0-000393876156cox.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

    Robert Klemme Guest

  3. #3

    Default Re: How do I use grep with the Shell class

    On Wed, 17 Sep 2003 14:13:57 +0900
    Mark Wilson <mwilson13cox.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
    Jason Creighton Guest

Similar Threads

  1. using grep in perl
    By Andrew Gaffney in forum PERL Beginners
    Replies: 3
    Last Post: February 3rd, 05:03 PM
  2. perl grep..
    By John W. Krahn in forum PERL Beginners
    Replies: 0
    Last Post: January 16th, 11:30 PM
  3. grep with ftp
    By Shaunn Johnson in forum PERL Beginners
    Replies: 9
    Last Post: September 12th, 06:51 AM
  4. grep
    By Anders Bystrup in forum PERL Miscellaneous
    Replies: 3
    Last Post: August 7th, 09:36 PM
  5. error message for Shell class
    By s moon in forum Ruby
    Replies: 2
    Last Post: August 1st, 02:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139