PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Add new text to files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Add new text to files

Réponse
 
LinkBack Outils de la discussion
Vieux 18/06/2008, 23h52   #1
Chris Conley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Add new text to files

Hello,

I'm trying to insert new text into the middle of a file with the
following:

file = File.open("file.rb", "r+")
file.each { |line|
if line.match(/line text/)
file.puts("hello")
end
}

This works fine except that it overwrites the existing 5 characters to
replace with "hello". Is there a way to insert a new line with new
text without overwriting any existing data?

Thanks!
Chris

  Réponse avec citation
Vieux 19/06/2008, 00h29   #2
Simon Blanco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Add new text to files

Chris Conley wrote:
> Hello,
>
> I'm trying to insert new text into the middle of a file with the
> following:
>
> file = File.open("file.rb", "r+")
> file.each { |line|
> if line.match(/line text/)
> file.puts("hello")
> end
> }
>
> This works fine except that it overwrites the existing 5 characters to
> replace with "hello". Is there a way to insert a new line with new
> text without overwriting any existing data?
>
> Thanks!
> Chris


try changing:
file = File.open("file.rb", "r+")

for:
file = File.open("file.rb", "a+")


that should do it!

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

  Réponse avec citation
Vieux 19/06/2008, 00h37   #3
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Add new text to files

On Jun 18, 2008, at 7:29 PM, Simon Blanco wrote:
> Chris Conley wrote:
>> Hello,
>>
>> I'm trying to insert new text into the middle of a file with the
>> following:
>>
>> file = File.open("file.rb", "r+")
>> file.each { |line|
>> if line.match(/line text/)
>> file.puts("hello")
>> end
>> }
>>
>> This works fine except that it overwrites the existing 5 characters
>> to
>> replace with "hello". Is there a way to insert a new line with new
>> text without overwriting any existing data?
>>
>> Thanks!
>> Chris

>
> try changing:
> file = File.open("file.rb", "r+")
>
> for:
> file = File.open("file.rb", "a+")
>
> that should do it!



Uh, no, that would just cause the writes to go the end of the file.

You have to open up space in the file to do this. The easiest way is
to copy lines from the original file and put the possibly altered
lines into a new file. If you want, you can rename the files when
you're done (and delete the original).

You might also want to look at the ruby command line flag '-i' and see
if that gives you some ideas.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com



  Réponse avec citation
Vieux 19/06/2008, 06h40   #4
luka luka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Add new text to files

Chris Conley wrote:
> Hello,
>
> I'm trying to insert new text into the middle of a file with the
> following:
>
> file = File.open("file.rb", "r+")
> file.each { |line|
> if line.match(/line text/)
> file.puts("hello")
> end
> }
>
> This works fine except that it overwrites the existing 5 characters to
> replace with "hello". Is there a way to insert a new line with new
> text without overwriting any existing data?


Hello Chris (:
Try count lines, and when line = (what do you want, exmaple 10),
continue puts into the file.

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

  Réponse avec citation
Vieux 19/06/2008, 19h43   #5
Bryan JJ Buckley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Add new text to files

Hi,
To edit a file in place, you could try something like

File.open('file.rb', 'r+') do |file|
file.each do |line|
if line =~ /line text/
file.seek(-line.size, IO::SEEK_CUR)
file.puts("Hello")
end
end
end

Note the minus before line.size. This will probably fall over on
non-ascii files, as you're seeking back too little (String#size
returns the number of letters).

However, this is a C-ish approach, and unless you want your programme
to run on a tamagotchi, or something, you're probably better off just
reading in one file, and writing to another.



2008/6/19 Chris Conley <chris.m.conley@gmail.com>:
> Hello,
>
> I'm trying to insert new text into the middle of a file with the
> following:
>
> file = File.open("file.rb", "r+")
> file.each { |line|
> if line.match(/line text/)
> file.puts("hello")
> end
> }
>
> This works fine except that it overwrites the existing 5 characters to
> replace with "hello". Is there a way to insert a new line with new
> text without overwriting any existing data?
>
> Thanks!
> Chris
>
>




--
JJ

  Réponse avec citation
Vieux 19/06/2008, 21h12   #6
Chris Conley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Add new text to files

[Note: parts of this message were removed to make it a legal post.]

Hi all, thanks for the info!

I ended up finding this solution from digging through some Rails generators:

def gsub_file(path, regexp, *args, &block)
content = File.read(path).gsub(regexp, *args, &block)
File.open(path, 'wb') { |file| file.write(content) }
end

line = 'Rails::Initializer.run do |config|'
gsub_file 'config/environment.rb', /(#{Regexp.escape(line)})/mi do |match|
"#{match}\n hello\n"
end

It basically reads the file, finds and replaces the line with itself and
whatever you want to add in to 'content'. Then writes back to the file.

On Thu, Jun 19, 2008 at 2:43 PM, Bryan JJ Buckley <jjbuckley@gmail.com>
wrote:

> Hi,
> To edit a file in place, you could try something like
>
> File.open('file.rb', 'r+') do |file|
> file.each do |line|
> if line =~ /line text/
> file.seek(-line.size, IO::SEEK_CUR)
> file.puts("Hello")
> end
> end
> end
>
> Note the minus before line.size. This will probably fall over on
> non-ascii files, as you're seeking back too little (String#size
> returns the number of letters).
>
> However, this is a C-ish approach, and unless you want your programme
> to run on a tamagotchi, or something, you're probably better off just
> reading in one file, and writing to another.
>
>
>
> 2008/6/19 Chris Conley <chris.m.conley@gmail.com>:
> > Hello,
> >
> > I'm trying to insert new text into the middle of a file with the
> > following:
> >
> > file = File.open("file.rb", "r+")
> > file.each { |line|
> > if line.match(/line text/)
> > file.puts("hello")
> > end
> > }
> >
> > This works fine except that it overwrites the existing 5 characters to
> > replace with "hello". Is there a way to insert a new line with new
> > text without overwriting any existing data?
> >
> > Thanks!
> > Chris
> >
> >

>
>
>
> --
> JJ
>
>



--
Chris Conley
484.467.7873
@chrisconley

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19069 seconds with 14 queries