# Search keywords inside of directories and files

**URL:** <https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221>\
**Category:** Ruby\
**Created:** [August 22, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221 "2009-08-22T21:35:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 22, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221/1 "2009-08-22T21:35:51Z")

</div>

I need some help about keywords searching in directory and files.

I have a directory  
dirs–a  
|–b  
|–c  
and files  
a–\>a1.txt  
|–\>a2.txt  
|–\>a3.txt

I can lsit all files and directories like this,

require ‘find’  
dirs = [‘C:\Users\test\dirs’]  
keywords = [“keyword1”,“keyword2”]  
for dir in dirs  
Find.find(dir) do |path|  
if FileTest.directory?(path)  
if keywords.include?(File.basename(path))  
# make something here… and output the finding data  
else  
Find.prune # skip the files  
next  
end  
else  
puts path  
end  
end  
end

how can I do it and how can I print output the results in a text or csv  
file?  
Or need a plugin like Ferret?  
please help me.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 27, 2009, 12:53am UTC](https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221/2 "2009-08-27T00:53:24Z")

</div>

Ahmet K. wrote:

> I need some help about keywords searching in directory and files.
> 
> I have a directory  
> dirs–a  
> |–b  
> |–c  
> and files  
> a–\>a1.txt  
> |–\>a2.txt  
> |–\>a3.txt
> 
> I can lsit all files and directories like this,
> 
> require ‘find’  
> dirs = [‘C:\Users\test\dirs’]  
> keywords = [“keyword1”,“keyword2”]  
> for dir in dirs  
> Find.find(dir) do |path|  
> if FileTest.directory?(path)  
> if keywords.include?(File.basename(path))  
> # make something here… and output the finding data  
> else  
> Find.prune # skip the files  
> next  
> end  
> else  
> puts path  
> end  
> end  
> end
> 
> how can I do it and how can I print output the results in a text or csv  
> file?  
> Or need a plugin like Ferret?  
> please help me.

To write to a file you need something like this:  
f=File.new(“w”,“myfile.txt”)  
f \<\< “line 1”  
f \<\< “line 2”  
f.close

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 27, 2009, 3:40am UTC](https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221/3 "2009-08-27T03:40:26Z")

</div>

Rodrigo B. wrote:

> > To write to a file you need something like this:  
> > f=File.new(“w”,“myfile.txt”)  
> > f \<\< “line 1”  
> > f \<\< “line 2”  
> > f.close  
> > Sorry it should be filename, mode …this way is the correct.  
> > f=File.new(“myfile.txt”,"w)

It is OK. Thank you very much.  
I don’t want to print the whole line,  
I want to print some sql special words,  
for example;  
1- if the line start with comment(//) do not print the line.  
2- if line contain “find” and I want to print the leftside of “::” or  
class name (like this ClassBase::Class print ClassBase)  
3- if line contain “find” and I want to print the sql commands (like  
insert update …), table and column names. ( like this; insert into  
tableA where  
columnA, columnB, columnC… print he TableA and ColumnA, columnB,  
columnCã€€…)

please help me.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 27, 2009, 12:54am UTC](https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221/4 "2009-08-27T00:54:40Z")

</div>

> To write to a file you need something like this:  
> f=File.new(“w”,“myfile.txt”)  
> f \<\< “line 1”  
> f \<\< “line 2”  
> f.close  
> Sorry it should be filename, mode …this way is the correct.  
> f=File.new(“myfile.txt”,"w)

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 27, 2009, 8:30am UTC](https://www.ruby-forum.com/t/search-keywords-inside-of-directories-and-files/174221/5 "2009-08-27T08:30:22Z")

</div>

Ahmet K. wrote:

> Rodrigo B. wrote:
> 
> > > To write to a file you need something like this:  
> > > f=File.new(“w”,“myfile.txt”)  
> > > f \<\< “line 1”  
> > > f \<\< “line 2”  
> > > f.close  
> > > Sorry it should be filename, mode …this way is the correct.  
> > > f=File.new(“myfile.txt”,"w)
> 
> It is OK. Thank you very much.  
> I don’t want to print the whole line,  
> I want to print some sql special words,  
> for example;  
> 1- if the line start with comment(//) do not print the line.  
> 2- if line contain “find” and I want to print the leftside of “::” or  
> class name (like this ClassBase::Class print ClassBase)  
> 3- if line contain “find” and I want to print the sql commands (like  
> insert update …), table and column names. ( like this; insert into  
> tableA where  
> columnA, columnB, columnC… print he TableA and ColumnA, columnB,  
> columnCã€€…)
> 
> please help me.

what I done so far;

1- i think it OK.  
2- It almost done but I can do print it with this code  
text[/(D._)+::/], if the line contains second :: it print also. I  
dontwant it.  
3- maybe 50 percent, i can print  
text.grep( /FROM\s+(._?)\s+WHERE/ ) do |line|  
puts line  
end

but it is not what I want.

please help me!..
