[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

replace a text in base directory

Pokkai Dokkai

10/5/2007 9:49:00 AM

how can replace a text from all files in
"/home/user" directory and sub directories (tree)...
--
Posted via http://www.ruby-....

7 Answers

7stud 7stud

10/5/2007 12:33:00 PM

0

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

Pokkai Dokkai

10/5/2007 1:50:00 PM

0

fine
thanks 7stud
--
Posted via http://www.ruby-....

William James

10/5/2007 2:57:00 PM

0

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)
> end
>
> File.delete(fname)
> File.rename("temp.txt", fname)
> end
> end

This botched code bombs. Don't ever post untest code without
stating that it is untested code.

7stud 7stud

10/5/2007 3:33:00 PM

0

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

John Joyce

10/5/2007 11:01:00 PM

0


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-....
>
What you want a EULA(gy)?

M. Edward (Ed) Borasky

10/6/2007 2:26:00 AM

0

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!"

Suraj Kurapati

10/6/2007 3:08:00 AM

0

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