how can replace a text from all files in
"/home/user" directory and sub directories (tree)...
···
--
Posted via http://www.ruby-forum.com/.
how can replace a text from all files in
"/home/user" directory and sub directories (tree)...
--
Posted via http://www.ruby-forum.com/.
Pokkai Dokkai wrote:
how can replace a text from all files in
"/home/user" directory and sub directories (tree)...
Try this:
Dir.glob('/Users/me/2testing/dir2/**/*') do |fname|
next if File.directory?(fname)
File.open("temp.txt", "w") do |temp_file|
IO.foreach(fname) do |line| #fname is from outer loop
new_line = line.gsub("hello world", "goodbye")
temp_file.print(new_line)
end
File.delete(fname)
File.rename("temp.txt", fname)
end
end
** means to look in the current directory and all subdirectories for
the filenames *, where * means all file names(including directory
names).
--
Posted via http://www.ruby-forum.com/\.
Pokkai Dokkai wrote:
how can replace a text from all files in
"/home/user" directory and sub directories (tree)...
Using a little UNIX magic, you can get away with a shorter solution:
find /home/user -type f -print0 | xargs -0 \
ruby -pe 'gsub /pattern/, "replacement"' -i.bak
The "-i" will modify the file in-place and the ".bak" after it will make
a backup of the original file with a ".bak" suffix.
You could also use sed, awk, perl, etc. in place of 'ruby' in the same
command.
--
Posted via http://www.ruby-forum.com/\.
This botched code bombs. Don't ever post untest code without
stating that it is untested code.
On Oct 5, 7:33 am, 7stud -- <dol...@excite.com> wrote:
Pokkai Dokkai wrote:
> how can replace a text from all files in
> "/home/user" directory and sub directories (tree)...Try this:
Dir.glob('/Users/me/2testing/dir2/**/*') do |fname|
next if File.directory?(fname)File.open("temp.txt", "w") do |temp_file|
IO.foreach(fname) do |line| #fname is from outer loop
new_line = line.gsub("hello world", "goodbye")
temp_file.print(new_line)
endFile.delete(fname)
File.rename("temp.txt", fname)
end
end
William James wrote:
This botched code bombs. Don't ever post untest code without
stating that it is untested code.
lol.
--
Posted via http://www.ruby-forum.com/\.
What you want a EULA(gy)?
On Oct 5, 2007, at 10:33 AM, 7stud -- wrote:
William James wrote:
This botched code bombs. Don't ever post untest code without
stating that it is untested code.lol.
--
Posted via http://www.ruby-forum.com/\.
William James wrote:
>
> This botched code bombs. Don't ever post untest code without
> stating that it is untested code.
>
My god... you ran code posted to a mailing list blindly?!?
I made that mistake... once.
~Wayne
Wayne E. Seguin wrote:
William James wrote:
This botched code bombs. Don't ever post untest code without
stating that it is untested code.My god... you ran code posted to a mailing list blindly?!?
I made that mistake... once.~Wayne
puts "Whiner!"