# Ruby Command Line One Liner

**URL:** <https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715>\
**Category:** Ruby\
**Created:** [January 1, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715 "2009-01-01T20:52:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 1, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/1 "2009-01-01T20:52:03Z")

</div>

Hi,

I try to write a Ruby one-liner that replaces the header information of  
some \*.java files.

My first try worked perfect for a single file. But I have no idea on how  
to change my one-liner to work with multiple files.

Here is my one-liner for a single file:

ruby -i.bak -p -e ‘puts ARGF.read.gsub(/.\*(?=package)/m,"/\*\*\n \* foo.bar  
2001-2009\n\ \*\*/\n")’ test/Test.java

Does anybody have a idea on how to solve this problem for all \*.java  
files in the test directory?

Thanks for your answer,  
Alain.

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 1, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/2 "2009-01-01T20:58:47Z")

</div>

Hi –

On Fri, 2 Jan 2009, Alain H. wrote:

> ruby -i.bak -p -e ‘puts ARGF.read.gsub(/.\*(?=package)/m,“/\*\*\n \* foo.bar  
> 2001-2009\n\ \*\*/\n”)’ test/Test.java
> 
> Does anybody have a idea on how to solve this problem for all \*.java  
> files in the test directory?

The -p flag will do the read/write loop for you, so all you have to do  
is something like this:

$ cat abc.txt  
hello  
$ cat def.txt  
goodbye

$ ruby -pi.bak -e ‘$\_.upcase!’ abc.txt def.txt

$ cat abc.txt  
HELLO  
$ cat def.txt  
GOODBYE

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 1, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/3 "2009-01-01T21:35:47Z")

</div>

On Thu, Jan 1, 2009 at 11:51 AM, Alain H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I try to write a Ruby one-liner that replaces the header information of  
> some \*.java files.
> 
> My first try worked perfect for a single file. But I have no idea on how  
> to change my one-liner to work with multiple files.

You might be interested in this: [http://rush.heroku.com/](http://rush.heroku.com/)

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 4, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/4 "2009-01-04T22:29:44Z")

</div>

Hi –

On Fri, 2 Jan 2009, Alain H. wrote:

> > $ cat abc.txt  
> > Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))  
> > /\*\*  
> > package test;  
> > …
> 
> If I use the one-liner at the top of the message with an input like  
> ‘test/Tes\*.java’ then the ARGF stream does somehow not differ between  
> the different files.

It sounds like you might want -n rather than -p. -n does the same  
loop, but without printing. You could arrange to print once you’ve hit  
/package/.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 2, 2009, 11:03am UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/5 "2009-01-02T11:03:26Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Fri, 2 Jan 2009, Alain H. wrote:
> 
> > ruby -i.bak -p -e ‘puts ARGF.read.gsub(/.\*(?=package)/m,“/\*\*\n \* foo.bar  
> > 2001-2009\n\ \*\*/\n”)’ test/Test.java
> > 
> > Does anybody have a idea on how to solve this problem for all \*.java  
> > files in the test directory?
> 
> The -p flag will do the read/write loop for you, so all you have to do  
> is something like this:
> 
> $ cat abc.txt  
> hello  
> $ cat def.txt  
> goodbye
> 
> $ ruby -pi.bak -e ‘$\_.upcase!’ abc.txt def.txt
> 
> $ cat abc.txt  
> HELLO  
> $ cat def.txt  
> GOODBYE
> 
> David
> 
> –  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))
> 
> [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

Thanks a lot for your answer.

The problem of the -p flag is, that the input stream is read line by  
line. Then the regex /.\*(?=package)/ will only be applied to one line.  
But I want to remove all lines before the line starting with “package”.  
See the following example:

/\*\*

- header  
\*\*/  
package test;  
…

The result of the replacement looks like this:

/\*\*

- foo.bar  
\*\*/  
package test;  
…

If I use the one-liner at the top of the message with an input like  
‘test/Tes\*.java’ then the ARGF stream does somehow not differ between  
the different files.

Best regards, Alain.

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 5, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/6 "2009-01-05T21:11:00Z")

</div>

On Jan 1, 1:51 pm, Alain H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ruby -i.bak -p -e ‘puts ARGF.read.gsub(/.\*(?=package)/m,“/\*\*\n \* foo.bar  
> 2001-2009\n\ \*\*/\n”)’ test/Test.java
> 
> Does anybody have a idea on how to solve this problem for all \*.java  
> files in the test directory?
> 
> ## Thanks for your answer, Alain.
> 
> Posted viahttp://www.ruby-forum.com/.

ruby -i.bak -0777 -pe"sub /.\*foo/m,‘bar’" \*.java

---

<div class="post-metadata">

**Author:** ![helpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@helpy](https://www.ruby-forum.com/u/helpy)\
**Post date:** [January 11, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/ruby-command-line-one-liner/156715/7 "2009-01-11T20:25:49Z")

</div>

unknown wrote:

> On Jan 1, 1:51ï¿½pm, Alain H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ruby -i.bak -p -e ‘puts ARGF.read.gsub(/.\*(?=package)/m,“/\*\*\n \* foo.bar  
> > 2001-2009\n\ \*\*/\n”)’ test/Test.java
> > 
> > Does anybody have a idea on how to solve this problem for all \*.java  
> > files in the test directory?
> > 
> > ## Thanks for your answer, Alain.
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> ruby -i.bak -0777 -pe"sub /.\*foo/m,‘bar’" \*.java

Thanks a lot for the help.

The -0777 flag was the key to success, Alain.
