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 > "OneLiner" - replacement
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
"OneLiner" - replacement

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 14h50   #1
Christian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut "OneLiner" - replacement

Hi,

i have some replacement's and would like to use the "in place
operator".
How is it possible doing more than one repalcement, i thought i read
something
to hang the gsub method one after another, but it dosn't work!?


ruby -pi*.bak -e 'gsub(/VAR_TYP/,"TYP").gsub(/VAR_FREQ/,"FREQ")'
file.dat

many thanks
Christian
  Réponse avec citation
Vieux 25/02/2008, 15h55   #2
Mark Bush
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "OneLiner" - replacement

Christian Schulz wrote:
> ruby -pi*.bak -e 'gsub(/VAR_TYP/,"TYP").gsub(/VAR_FREQ/,"FREQ")'
> file.dat


You don't want the '*' after the 'i' flag (I suspect).

Replace the '.' by a ';' between the two calls to gsub or replace the
two calls to gsub by calls to gsub! (keeping your '.') and you should be
ok.

Note also that both your substitutions are doing the same thing
(removing VAR_) so you can do it in one:

ruby -pi.bak -e 'gsub /VAR_(TYP|FREQ)/, "\\1" ' file.dat

(The double backslash here is required because of the use of double
quotes. To avoid that, put the quotes the other way round:

ruby -pi.bak -e "gsub /VAR_(TYP|FREQ)/, '\1' " file.dat

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

  Réponse avec citation
Vieux 25/02/2008, 15h59   #3
Mark Bush
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "OneLiner" - replacement

Mark Bush wrote:
> Replace the '.' by a ';' between the two calls to gsub or replace the
> two calls to gsub by calls to gsub! (keeping your '.') and you should be
> ok.


Actually, forget chaining the calls to gsub! as you'll bomb if no sub is
made. Sorry.

Just use separate call to gsub rather than chaining them.

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

  Réponse avec citation
Vieux 25/02/2008, 16h01   #4
Mark Thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "OneLiner" - replacement

First, I don't think the * is doing what you think it is. 'ruby -
pi.bak' is probably what you want. Second, it's more efficient to use
one regex when you don't really need two.

ruby -pi.bak -e 'gsub(/VAR_(TYP|FREQ)/,'\1')' file.dat

  Réponse avec citation
Vieux 25/02/2008, 17h13   #5
Christian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "OneLiner" - replacement

On 25 Feb., 17:01, Mark Thomas <m...@thomaszone.com> wrote:
> First, I don't think the * is doing what you think it is. 'ruby -
> pi.bak' is probably what you want. Second, it's more efficient to use
> one regex when you don't really need two.
>
> ruby -pi.bak -e 'gsub(/VAR_(TYP|FREQ)/,'\1')' file.dat


many thanks's , it's too bad but i have a lot of replacment's
difficult to combine.
The hint for ";" instead of "." is my solution and now i recognize why
my replaced file end with $$$ instead of *.bak
like in perl.

regards, Christian
  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 03h37.


É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,08653 seconds with 13 queries