Search directory for containing specified string

I am trying to search a directory for all files that contain a specific
string. My code can be found below. Currently, there are no errors, but
it is not giving me filenames for files containing the text string I am
looking for.

search_text = %r{
.refreshrates().|
.orderclose.
}x
files = Dir.glob("*.MQ4") {|filename|
File.open(filename) do |outfile|
outfile.each_line do |line|
if line =~ search_text then
puts filename
end
end
end
}

On 2/3/2011 3:37 PM, Bob H. wrote:

I am trying to search a directory for all files that contain a specific
string. My code can be found below. Currently, there are no errors, but
it is not giving me filenames for files containing the text string I am
looking for.

Hi, Bob. So far you appear to be reimplementing the grep tool in Ruby.
It’s able to filter files based on regular expressions as well as
report files whose contents match regular expressions. There’s nothing
wrong with doing that, but you do know that you can get grep for
Windows, right? It seems to be the right tool for the job here.

    end
  end

}

Have you confirmed that your script is actually processing any files at
all? Maybe your glob isn’t matching anything. Try adding a puts
filename as the first statement of the block given to File.open. If you
don’t see anything there, check your glob.

I also notice that your regexp includes parenthesis and dots. Those
have special meaning in regexps, so you need to escape each of them with
a single backslash () if you want them to match literally.

Another thing I notice is that you are assigning the result of running
Dir.glob to the files variable. Given what you’re doing in this script,
you don’t need that at all. Just call Dir.glob without assigning its
result to anything.

-Jeremy

check to make sure MQ4 are text files. I suspect its an executable file
so most probably you won’t be able to directly grep from it. I might be
wrong.

On Thu, Feb 3, 2011 at 10:37 PM, Bob H. [email protected] wrote:

I am trying to search a directory for all files that contain a specific
string. My code can be found below. Currently, there are no errors, but
it is not giving me filenames for files containing the text string I am
looking for.

search_text = %r{
.refreshrates().|

Do you want to look for literal parentheses? Otherwise this empty
capturing group is obsolete. Not that it should prevent matches
though.

.orderclose.
}x
files = Dir.glob("*.MQ4") {|filename|

Are file extensions really uppercase? If unsure you could as well do

Dir.glob “*.{MQ,mq}4”

File.open(filename) do |outfile|
outfile.each_line do |line|
if line =~ search_text then
puts filename
end
end
end
}

Hm, even if they are not text files you may be able to find a match.
I would not bet on it though - mixing binary data with regular
expressions is not exactly intended use. :slight_smile:

You could compare results with

egrep -l ‘refreshrates|orderclose’ *.MQ4

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs