[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

"OneLiner" - replacement

chris

2/25/2008 2:51:00 PM

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
4 Answers

Mark Bush

2/25/2008 3:55:00 PM

0

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

Mark Bush

2/25/2008 4:00:00 PM

0

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

mrt

2/25/2008 4:01:00 PM

0

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

chris

2/25/2008 5:14:00 PM

0

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