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 > changing a files' content
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
changing a files' content

Réponse
 
LinkBack Outils de la discussion
Vieux 04/12/2007, 09h18   #1
Paul Fox
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut changing a files' content

hi all,
i have a text file with various lines, and i want to alter some of these
using regex and gsub.

so i have patterns={/some_pattern/=>"replacement text"}
and i'm thinking this:
in = File.open(filename)
while (line=in.gets)
if line.strip =~ pattern
line.gsub(pattern,patterns[pattern])
end
end

but i'm not sure if that's efficient? and of course, i'm struggling on
the best way of writing those changes back into the file.

any thoughts?
thanks,
f.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 04/12/2007, 10h59   #2
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing a files' content

On Dec 4, 2007 10:18 AM, Paul Fox <olifoxpaul@googlemail.com> wrote:
> hi all,
> i have a text file with various lines, and i want to alter some of these
> using regex and gsub.
>
> so i have patterns={/some_pattern/=>"replacement text"}
> and i'm thinking this:
> in = File.open(filename)
> while (line=in.gets)
> if line.strip =~ pattern
> line.gsub(pattern,patterns[pattern])
> end
> end


Maybe you would like to do this

ARGF.each | line |
line.gsub(...) if line.....
print line
end

and then
ruby -i.bup source.rb file1, ...


HTH
Robert

--

http://ruby-smalltalk.blogspot.com/

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

  Réponse avec citation
Vieux 04/12/2007, 11h11   #3
Paul Fox
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing a files' content

Robert Dober wrote:
> ruby -i.bup source.rb file1, ...


thanks for the reply the -i flag looks like it'll be useful, but in my
current situation, i need to be able to do it via an instance method,
rather than command line.
i guess i could call system() to do it, but tht doesn't seem right?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 04/12/2007, 12h22   #4
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing a files' content

On Dec 4, 2007 12:11 PM, Paul Fox <olifoxpaul@googlemail.com> wrote:
> Robert Dober wrote:
> > ruby -i.bup source.rb file1, ...

>
> thanks for the reply the -i flag looks like it'll be useful, but in my
> current situation, i need to be able to do it via an instance method,
> rather than command line.

I fail to understand, can you elaborate on the calling environment?
Assuming that you cannot use the -i flag I would be tempted to read
the file into memory or use tempfile to create a copy of the original
file and than open the original file with write access ("w"), if this
is not feasible either I would (as a last resort ) go for read-write
access, this is quite messy at least FWIAC .
Hopefully somebody else can give you (us) better information about
that particular issue.

Robert
> i guess i could call system() to do it, but tht doesn't seem right?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>




--

http://ruby-smalltalk.blogspot.com/

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

  Réponse avec citation
Vieux 04/12/2007, 15h07   #5
MonkeeSage
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing a files' content

On Dec 4, 3:18 am, Paul Fox <olifoxp...@googlemail.com> wrote:
> hi all,
> i have a text file with various lines, and i want to alter some of these
> using regex and gsub.
>
> so i have patterns={/some_pattern/=>"replacement text"}
> and i'm thinking this:
> in = File.open(filename)
> while (line=in.gets)
> if line.strip =~ pattern
> line.gsub(pattern,patterns[pattern])
> end
> end
>
> but i'm not sure if that's efficient? and of course, i'm struggling on
> the best way of writing those changes back into the file.
>
> any thoughts?
> thanks,
> f.
> --
> Posted viahttp://www.ruby-forum.com/.


patterns = { /some_pattern/ => "replacement text" }

File.open(filename, 'r+') { | handle |
out = []
handle.readlines { | line |
patterns.each { | pattern, replacement |
if line.strip =~ pattern
line = line.gsub(pattern, replacement)
end
}
out << line
}
handle.write(out.join(''))
}

Untested...

Regards,
Jordan
  Réponse avec citation
Vieux 07/12/2007, 21h31   #6
lrlebron@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing a files' content

On Dec 4, 3:18 am, Paul Fox <olifoxp...@googlemail.com> wrote:
> hi all,
> i have a text file with various lines, and i want to alter some of these
> using regex and gsub.
>
> so i have patterns={/some_pattern/=>"replacement text"}
> and i'm thinking this:
> in = File.open(filename)
> while (line=in.gets)
> if line.strip =~ pattern
> line.gsub(pattern,patterns[pattern])
> end
> end
>
> but i'm not sure if that's efficient? and of course, i'm struggling on
> the best way of writing those changes back into the file.
>
> any thoughts?
> thanks,
> f.
> --
> Posted viahttp://www.ruby-forum.com/.


I did something similar using the rio library. Cleaned up some vb
files in place

require 'rio'
rio('C://test').all.files('*.vb') do |f|
rio(f) <f.contents.gsub(/'UPGRADE.*\s/,"").gsub(/On Error GoTo
ErrHandler/,"Try").gsub(/ErrHandler:/,"Catch ex as Exception\n").gsub(/
Resume Next/,"End Try")
end
  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 03h03.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13707 seconds with 14 queries