[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: open files in a dir then gsub all of them?

Ezra Zygmuntowicz

3/29/2005 7:13:00 PM

Hey there list-
Can someone help out a n00b with a simple problem. What is the proper
way to open all text files in a directory and the call gsub on each
file? Thanks in advance.

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra@yakima-herald.com
2 Answers

Bill Kelly

3/29/2005 9:08:00 PM

0

Hi,

From: "Ezra Zygmuntowicz" <ezra@yakima-herald.com>
>
> Can someone help out a n00b with a simple problem. What is the proper
> way to open all text files in a directory and the call gsub on each
> file? Thanks in advance.

This is something you can even do from the command line:

ruby -i~ -pe 'gsub(/foo/,"bar")' *.baz

The above should change foo to bar in all *.baz files, and
save the original files as *.baz~

Or, without using the command line:

files = Dir["*.txt"]

files.each do |filename|
content = File.read(filename) # note: File#read is ruby 1.8+
if content.gsub!(/foo/, "bar")
File.open(filename, "w") {|io| io.print content }
end
end

The above is untested, but should read each .txt file, perform
the gsub, and only write the file back if anything changed.

Hope this helps,

Regards,

Bill




The Walrus was Danny

1/5/2010 12:50:00 AM

0

On 5 Jan, 00:35, Mack A. Damia <mybaconbu...@hotmail.com> wrote:
> On Mon, 4 Jan 2010 15:51:17 -0800 (PST), The Walrus was Danny
>
> <dannyisthewal...@tesco.net> wrote:
> >http://www.facebook.com/photo_search.php?oid=24945607432&id......
>
> >God knows if you can see it but here is a picture of me looking like a
> >superstar in my new hat..which I bought thanks to tips from the good
> >people of rmb. I'll wear it from now on.
>
> >I have uploaded the pic onto the Facebook Group "rec.music.beatles"
>
> >The other day I was looking for a hat (obviously it was impossible to
> >get one like this) in Brighton and I saw a woman with a kind of
> >similar hat (though not as fantastic as this one) and I actually
> >stopped her and said "Where did you get that Hat"?...she said she got
> >it in Rye..which is a bit far...it was then that I turned to your
> >goodselves and the magic that is the Internet.
>
> Very spiffy!
>
> I used to wear hats such as that one all the time.  There used to be a
> great hat shop on 8th Avenue directly across from Port Authority.  It
> may be still there.
>
> I still wear those type occasionally, especially when I go for a fast
> walk - but I changed to fedoras and the like back in the 1980s. I have
> a funny story to tell you over a beer about a pigeon shitting on the
> brim of a beautiful James Dean Fedora I had just bought and was
> wearing in NYC.
>
> Let me know as soon as you book your flight, Danny, so I can plan my
> trip.

Of course..I think the first week in August will be the goer..but I
will clarify when I get my life sorted out...

Danny