# "OneLiner" - replacement

**URL:** <https://www.ruby-forum.com/t/oneliner-replacement/186728>\
**Category:** Ruby\
**Created:** [February 25, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/oneliner-replacement/186728 "2008-02-25T15:55:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [February 25, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/oneliner-replacement/186728/1 "2008-02-25T15:55:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [February 25, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/oneliner-replacement/186728/2 "2008-02-25T16:55:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [February 25, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/oneliner-replacement/186728/3 "2008-02-25T16:59:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [February 25, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/oneliner-replacement/186728/4 "2008-02-25T17:05:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [February 25, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/oneliner-replacement/186728/5 "2008-02-25T18:16:52Z")

</div>

On 25 Feb., 17:01, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
