Ruby Command Line One Liner

  • Thread starter Alain Helfenstein
  • Start date
A

Alain Helfenstein

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

David A. Black

Hi --

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?

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
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
H

Hassan Schroeder

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/>
 
A

Alain Helfenstein

David said:
Hi --



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
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

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

w_a_x_man

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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top