Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby Command Line One Liner

Reply
Thread Tools

Ruby Command Line One Liner

 
 
Alain Helfenstein
Guest
Posts: n/a
 
      01-01-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      01-01-2009
Hi --

On Fri, 2 Jan 2009, Alain Helfenstein wrote:

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

 
Reply With Quote
 
 
 
 
Hassan Schroeder
Guest
Posts: n/a
 
      01-01-2009
On Thu, Jan 1, 2009 at 11:51 AM, Alain Helfenstein
<(E-Mail Removed)> 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/>

--
Hassan Schroeder ------------------------ http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Alain Helfenstein
Guest
Posts: n/a
 
      01-02-2009
David A. Black wrote:
> Hi --
>
> On Fri, 2 Jan 2009, Alain Helfenstein 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
> 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.

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

 
Reply With Quote
 
w_a_x_man@yahoo.com
Guest
Posts: n/a
 
      01-05-2009
On Jan 1, 1:51*pm, Alain Helfenstein <(E-Mail Removed)> wrote:
> 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.
> --
> Posted viahttp://www.ruby-forum.com/.


ruby -i.bak -0777 -pe"sub /.*foo/m,'bar'" *.java
 
Reply With Quote
 
Alain Helfenstein
Guest
Posts: n/a
 
      01-11-2009
unknown wrote:
> On Jan 1, 1:51�pm, Alain Helfenstein <(E-Mail Removed)> 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
windows one liner to output unix line feed boman Perl Misc 13 08-27-2009 06:58 PM
Single-liner for one-line substitute? Mike Pearson Perl Misc 13 06-30-2006 04:31 PM
perl one liner to display the third line from the end of a file Oxnard Perl Misc 13 06-15-2005 12:35 PM
one-liner to make all programs one-liners Larry Perl Misc 1 02-03-2005 11:35 PM
How do I do this one-liner (windows command) in perl ? Mark Perl Misc 8 04-29-2004 08:56 PM



Advertisments