Search directory for containing specified string

B

Bob Hatch

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
}
 
J

Jeremy Bopp

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

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
 
S

Steel Steel

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

Robert Klemme

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 =3D %r{
=A0.refreshrates().|

Do you want to look for literal parentheses? Otherwise this empty
capturing group is obsolete. Not that it should prevent matches
though.
=A0.orderclose.
}x
files =3D Dir.glob("*.MQ4") {|filename|

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

Dir.glob "*.{MQ,mq}4"
=A0 =A0 =A0File.open(filename) do |outfile|
=A0 =A0 =A0 =A0outfile.each_line do |line|
=A0 =A0if line =3D~ search_text then
=A0 =A0 =A0 =A0puts filename
=A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0end
=A0 =A0 =A0end
}

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

You could compare results with

egrep -l 'refreshrates|orderclose' *.MQ4

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top