Searching in a directory

Hello,

I want to search for an string in all .txt - files of an directory.
Could you give me sample code, please ??

···

--
Posted via http://www.ruby-forum.com/.

Can you provide some context/details, and perhaps some sample code for
what you are trying to accomplish?

···

On Fri, Aug 19, 2011 at 02:44:15AM +0900, Yu Yu wrote:

Hello,

I want to search for an string in all .txt - files of an directory.
Could you give me sample code, please ??

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

If you're on Unix:
$ find . -name '*.txt' | xargs grep 'your string here'

In Ruby:
Dir["**/*.txt"].each do |filename|
  lines = File.readlines(filename).grep(/your string here/)
  p filename => lines unless lines.empty?
end

···

On Thu, Aug 18, 2011 at 12:44 PM, Yu Yu <htwoo@web.de> wrote:

Hello,

I want to search for an string in all .txt - files of an directory.
Could you give me sample code, please ??

First of all thank you very much for help. I want to search in all .txt
files of a folder for a string and when I find this string, then i call
a function_1. If there's no string, I call a function_2. Later the
string will be dynamic, but that's not important now. I took the code
from Josh and tried a bit, but there's still a mistake concerning
reading the whole file.

Dir["**/*.txt"].each do |filename|
  if
    File.readlines(filename).include?("123"+"string") then
    puts("function_1")
  else
    puts("function_2")
  end
end

···

--
Posted via http://www.ruby-forum.com/.

when I find this string

Too nebulous.

···

--
Posted via http://www.ruby-forum.com/\.

I took the code
from Josh and tried a bit, but there's still a mistake
concerning reading the whole file.

...and what would that be?

···

--
Posted via http://www.ruby-forum.com/\.

I trying to call a process in function_1. And function_2 means to to
nothing.

···

--
Posted via http://www.ruby-forum.com/.

And here's how to do it using Virtual File System:

  require 'vfs'

  files_with_string = '.'.to_dir.files('*.txt').select do |file|
    file.read.include?('string to search')
  end

  p files_with_string

···

--
Posted via http://www.ruby-forum.com/.

Hello,
I still have problems and need your help!

I forgot to say that the algorithm should search the next string if the
actual string was found in one .txt file. The reason is that there're
some strings double.

This is my code, taken from 7stud:

  dir_path = 'c:/temp'
  str = "string"

  Dir.chdir(dir_path) do

  Dir.glob("*.txt") do |filename|
    IO.foreach(filename) do |line|
      if line =~ /#{str}/
        puts "#{filename}: #{str}"
        end
      end
    end
  end

···

--
Posted via http://www.ruby-forum.com/.

This search-routine is part of a bigger script and I've problems with
this part. I try to keep it simple...

I'm looking for one string: dog

in all these files:

file 1:
cat bird
frog eagle

file2:
dog cat
eagle frog

file3:
cat cat
eagle bird
dog bird

file4:
...

The search begins in file1. If the string dog is found in file2 the
search should stop. Thats all!

···

--
Posted via http://www.ruby-forum.com/.

Hi,

now it works and the method stands in the else-branch. There was a
problem with my database. It was not my intention to confuse you. Thx.

···

--
Posted via http://www.ruby-forum.com/.

Josh Cheek wrote in post #1017371:

In Ruby:
Dir["**/*.txt"].each do |filename|
  lines = File.readlines(filename).grep(/your string here/)
  p filename => lines unless lines.empty?
end

Or something like this:

dir_path = '/path/to/dir'
str = "some string"

Dir.chdir(dir_path) do
  Dir.glob("*") do |filename|
    IO.foreach(filename) do |line|
      if line =~ /#{str}/i
        puts "#{filename}: #{line}"
      end
    end
  end
end

···

--
Posted via http://www.ruby-forum.com/\.

First of all thank you very much for help. I want to search in all .txt
files of a folder for a string and when I find this string, then i call
a function_1. If there's no string, I call a function_2. Later the
string will be dynamic, but that's not important now. I took the code
from Josh and tried a bit, but there's still a mistake concerning
reading the whole file.

Dir["**/*.txt"].each do |filename|
  if
    File.readlines(filename).include?("123"+"string") then

This line, as written, is equivalent to saying:

    File.readlines(filename).include?("123string") then

Is that what you intended?

    puts("function_1")

Are you trying to call a method called function_1 or are you just trying
to output the string "function_1"?

···

On Fri, Aug 19, 2011 at 05:47:52AM +0900, Yu Yu wrote:

  else
    puts("function_2")
  end
end

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

I'm not sure what different action you plan to take if there is a double instance but:

Dir.glob("test.txt") do |filename|
   IO.foreach(filename).inject(nil) do |last_line, line|
     if line =~ /#{str}/
       puts "#{filename}: #{str}"
       if last_line == line
        puts "Double line"
       end
     end
     line
   end
end

This basically uses inject to keep a temporary holder of the last line, so you can check if it matches the previous one and take a different course of action for that case. The nil is passed as the initial value to inject so it won't match if the first string is blank.

Regards,
Chris White
http://www.twitter.com/cwgem

···

On Aug 19, 2011, at 12:27 PM, Yu Yu wrote:

dir_path = 'c:/temp'
str = "string"

Dir.chdir(dir_path) do

Dir.glob("*.txt") do |filename|
   IO.foreach(filename) do |line|
     if line =~ /#{str}/
       puts "#{filename}: #{str}"
       end
     end
   end
end

or just by changing one line if this helps with your situation:

if line =~ /(#{str})(?:.*\1)?/

Yu Yu wrote in post #1017568:

···

Hello,
I still have problems and need your help!

I forgot to say that the algorithm should search the next string if the
actual string was found in one .txt file. The reason is that there're
some strings double.

This is my code, taken from 7stud:

  dir_path = 'c:/temp'
  str = "string"

  Dir.chdir(dir_path) do

  Dir.glob("*.txt") do |filename|
    IO.foreach(filename) do |line|
      if line =~ /#{str}/
        puts "#{filename}: #{str}"
        end
      end
    end
  end

--
Posted via http://www.ruby-forum.com/\.

Yu Yu wrote in post #1017568:

Hello,
I still have problems and need your help!

I forgot to say that the algorithm should search the next string if the
actual string was found in one .txt file. The reason is that there're
some strings double.

There are string(s?) you are searching for, and their are lines you are
searching. It is not clear what you mean by 'next string'. Trying to
describe what you want is a waste of time when you can provide a simple
example instead. So you need to give an actual example, e.g.:

I'm looking for these strings:

dog
cat

in this file:

hello world
dog
hello world
cat

If dog matches one of the lines, then I want.....

···

--
Posted via http://www.ruby-forum.com/\.

Ah, here's a way to do that:

dir_path = './test_files'
str = "dog"

Dir.chdir(dir_path) do

Dir.glob("*.txt").inject(false) do |found, filename|
   IO.foreach(filename) do |line|
     if line =~ /#{str}/
       puts "#{filename}: #{str}"
       if found == true
        puts "Second instance found"
        exit # or however you want to handle this
       else
        found = true
       end
     end
   end
   found
end

end

This exits as soon as the second match is found, but you could have it run a method or something like that instead. You can also use throw/catch (note this is not about exceptions as with other languages using try/catch, it's meant to break out of deeply nested loops) to break out of the loop and go elsewhere.

Regards,
Chris White
http://www.twitter.com/cwgem

···

On Aug 19, 2011, at 1:51 PM, Yu Yu wrote:

This search-routine is part of a bigger script and I've problems with
this part. I try to keep it simple...

I'm looking for one string: dog

in all these files:

file 1:
cat bird
frog eagle

file2:
dog cat
eagle frog

file3:
cat cat
eagle bird
dog bird

file4:
...

The search begins in file1. If the string dog is found in file2 the
search should stop. Thats all!

Your requirements change with every post, and there are hidden
requirements that you don't even mention, and the requirements you do
mention aren't accurate.

Goodbye.

···

--
Posted via http://www.ruby-forum.com/.

Chad Perrin wrote in post #1017381:

Are you trying to call a method called function_1 or are you just trying
to output the string "function_1"?

It's irrelevant.

···

--
Posted via http://www.ruby-forum.com/\.

Chris White wrote in post #1017580:

···

On Aug 19, 2011, at 1:51 PM, Yu Yu wrote:
dir_path = './test_files'
str = "dog"

Dir.chdir(dir_path) do

Dir.glob("*.txt").inject(false) do |found, filename|
   IO.foreach(filename) do |line|
     if line =~ /#{str}/
       puts "#{filename}: #{str}"
       if found == true
        puts "Second instance found"
        exit # or however you want to handle this
       else
        found = true
       end
     end
   end
   found
end

end

This exits as soon as the second match is found, but you could have it
run a method or something like that instead.

Sorry, but there's still a problem with the code.
If I put a method instead of exit nothing happens. If I put a method
into the else-branch I only call the method if the string is double.

Remember: If the string dog is found in file2 the search should stop /
call a method.

--
Posted via http://www.ruby-forum.com/\.