Hello,
I want to search for an string in all .txt - files of an directory.
Could you give me sample code, please ??
Hello,
I want to search for an string in all .txt - files of an directory.
Could you give me sample code, please ??
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 ??
Can you provide some context/details, and perhaps some sample code for
what you are trying to accomplish?
On Thu, Aug 18, 2011 at 12:44 PM, Yu Yu [email protected] wrote:
Hello,
I want to search for an string in all .txt - files of an directory.
Could you give me sample code, please ??
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
Josh C. 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
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
On Fri, Aug 19, 2011 at 05:47:52AM +0900, Yu Yu wrote:
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”?
Chad P. 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.
when I find this string
Too nebulous.
I trying to call a process in function_1. And function_2 means to to
nothing.
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?
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
On Aug 19, 2011, at 12:27 PM, Yu Yu wrote:
end
end
end
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 W.
http://www.twitter.com/cwgem
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
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…
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!
On Aug 19, 2011, at 1:51 PM, Yu Yu wrote:
…
The search begins in file1. If the string dog is found in file2 the
search should stop. Thats all!
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 W.
http://www.twitter.com/cwgem
Chris W. 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
endend
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.
On Aug 19, 2011, at 3:11 PM, Yu Yu wrote:
puts "#{filename}: #{str}"
call a method.
Unfortunately without seeing what you have so far there’s no way for me
to pinpoint the issue.
Regards,
Chris W.
http://www.twitter.com/cwgem
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs