[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Quick sed replacemnt

Yitzhak Bar Geva

6/12/2007 7:14:00 PM

I need a quicky which I can't do in sed and I did it very clumsily.
I need to filter the following file, changing the class name's first
letter to upper case.
For example, rewrite the file with the word "organizationsController"
changed to "OrganizationsController".
Thanks.

Here's a sample file:

#
# A skeletal controller
#
class organizationsController < ApplicationController
active_scaffold :organization
# layout "activescaffold"
end


10 Answers

Reid Thompson

6/12/2007 7:21:00 PM

0

On Wed, 2007-06-13 at 04:14 +0900, yitzhakbg wrote:
> I need a quicky which I can't do in sed and I did it very clumsily.
> I need to filter the following file, changing the class name's first
> letter to upper case.
> For example, rewrite the file with the word "organizationsController"
> changed to "OrganizationsController".
> Thanks.
>
> Here's a sample file:
>
> #
> # A skeletal controller
> #
> class organizationsController < ApplicationController
> active_scaffold :organization
> # layout "activescaffold"
> end
>

sed 's/organizationsController/OrganizationsController/' file > newfile

Reid Thompson

6/12/2007 7:22:00 PM

0

On Wed, 2007-06-13 at 04:14 +0900, yitzhakbg wrote:
> I need a quicky which I can't do in sed and I did it very clumsily.
> I need to filter the following file, changing the class name's first
> letter to upper case.
> For example, rewrite the file with the word "organizationsController"
> changed to "OrganizationsController".
> Thanks.
>
> Here's a sample file:
>
> #
> # A skeletal controller
> #
> class organizationsController < ApplicationController
> active_scaffold :organization
> # layout "activescaffold"
> end
>
perl -pi -e 's/organizationsController/OrganizationsController/' file

Yitzhak Bar Geva

6/12/2007 7:26:00 PM

0

Sorry, You didn't understand. The class name can be anything. Whatever
it is, I want the first changed to upper case. If you can show me how
to do it in Sed, that's fine. I found it messy and tried writing a
Ruby script. It also came out a little messy. Can you help me?

On Jun 12, 10:20 pm, Reid Thompson <Reid.Thomp...@ateb.com> wrote:
> On Wed, 2007-06-13 at 04:14 +0900, yitzhakbg wrote:
> > I need a quicky which I can't do in sed and I did it very clumsily.
> > I need to filter the following file, changing the class name's first
> > letter to upper case.
> > For example, rewrite the file with the word "organizationsController"
> > changed to "OrganizationsController".
> > Thanks.
>
> > Here's a sample file:
>
> > #
> > # A skeletal controller
> > #
> > class organizationsController < ApplicationController
> > active_scaffold :organization
> > # layout "activescaffold"
> > end
>
> sed 's/organizationsController/OrganizationsController/' file > newfile


Gavin Kistner

6/12/2007 7:34:00 PM

0

On Jun 12, 1:26 pm, yitzhakbg <yitzha...@gmail.com> wrote:
> Sorry, You didn't understand. The class name can be anything. Whatever
> it is, I want the first changed to upper case. If you can show me how
> to do it in Sed, that's fine. I found it messy and tried writing a
> Ruby script. It also came out a little messy. Can you help me?

Take your pick:

class String
def upcase_first_letter_1
new_str = self.dup
new_str[ 0 ] = new_str[ 0..0 ].upcase
new_str
end
def upcase_first_letter_2
self.sub( /./ ){ |c| c.upcase }
end
def upcase_first_letter_3
self.sub( /[a-z]/i ){ |c| c.upcase }
end
end

%w| orgController _orgController |.each{ |n|
p n.upcase_first_letter_1,
n.upcase_first_letter_2,
n.upcase_first_letter_3
}
#=> "OrgController"
#=> "OrgController"
#=> "OrgController"
#=> "_orgController"
#=> "_orgController"
#=> "_OrgController"

Yitzhak Bar Geva

6/12/2007 7:36:00 PM

0

Thank you, but you still didn't get it. organizationscontroller is
only an example. It could be anything else

On Jun 12, 10:26 pm, yitzhakbg <yitzha...@gmail.com> wrote:
> Sorry, You didn't understand. The class name can be anything. Whatever
> it is, I want the first changed to upper case. If you can show me how
> to do it in Sed, that's fine. I found it messy and tried writing a
> Ruby script. It also came out a little messy. Can you help me?
>
> On Jun 12, 10:20 pm, Reid Thompson <Reid.Thomp...@ateb.com> wrote:
>
> > On Wed, 2007-06-13 at 04:14 +0900, yitzhakbg wrote:
> > > I need a quicky which I can't do in sed and I did it very clumsily.
> > > I need to filter the following file, changing the class name's first
> > > letter to upper case.
> > > For example, rewrite the file with the word "organizationsController"
> > > changed to "OrganizationsController".
> > > Thanks.
>
> > > Here's a sample file:
>
> > > #
> > > # A skeletal controller
> > > #
> > > class organizationsController < ApplicationController
> > > active_scaffold :organization
> > > # layout "activescaffold"
> > > end
>
> > sed 's/organizationsController/OrganizationsController/' file > newfile


Peter Cooper

6/12/2007 7:37:00 PM

0

On 6/12/07, yitzhakbg <yitzhakbg@gmail.com> wrote:
> Sorry, You didn't understand. The class name can be anything. Whatever
> it is, I want the first changed to upper case. If you can show me how
> to do it in Sed, that's fine. I found it messy and tried writing a
> Ruby script. It also came out a little messy. Can you help me?

perl -pi -e 's/^class ([a-z])/"class ".uc($1)/ge' filename.rb

works.. there is a nicer way to do the upper casing in a Perl s// but
I forget what, but this works ;-)

Cheers,
Peter Cooper
http://www.rubyi...

Peter Cooper

6/12/2007 7:39:00 PM

0

On 6/12/07, Peter Cooper <peter@peterc.org> wrote:
> On 6/12/07, yitzhakbg <yitzhakbg@gmail.com> wrote:
> > Sorry, You didn't understand. The class name can be anything. Whatever
> > it is, I want the first changed to upper case. If you can show me how
> > to do it in Sed, that's fine. I found it messy and tried writing a
> > Ruby script. It also came out a little messy. Can you help me?
>
> perl -pi -e 's/^class ([a-z])/"class ".uc($1)/ge' filename.rb

I remembered the nicer way to do it :)

perl -pi -e 's/^class ([a-z])/class \u\1/g' filename.rb

Cheers,
Peter Cooper
http://www.rubyi...

Bertram Scharpf

6/12/2007 10:20:00 PM

0

Hi,

Am Mittwoch, 13. Jun 2007, 04:14:28 +0900 schrieb yitzhakbg:
> I need a quicky which I can't do in sed and I did it very clumsily.
> I need to filter the following file, changing the class name's first
> letter to upper case.
> For example, rewrite the file with the word "organizationsController"
> changed to "OrganizationsController".

ruby -pe 'gsub(/organizationsController/){|x|x.capitalize}'
ruby -pe 'gsub(/\borganizationsController\b/){|x|x.capitalize}'

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Bertram Scharpf

6/13/2007 7:08:00 AM

0

Hi,

Am Mittwoch, 13. Jun 2007, 07:20:01 +0900 schrieb Bertram Scharpf:
> ruby -pe 'gsub(/organizationsController/){|x|x.capitalize}'
> ruby -pe 'gsub(/\borganizationsController\b/){|x|x.capitalize}'

__Sorry__! String#capitalize will destroy the camel case
spelling.

Here's another one:

ruby -i -pe 'gsub(/\bo(?=rganizationsController\b)/){|x|x.upcase}'

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Robert Klemme

6/13/2007 8:05:00 AM

0

On 12.06.2007 21:14, yitzhakbg wrote:
> I need a quicky which I can't do in sed and I did it very clumsily.
> I need to filter the following file, changing the class name's first
> letter to upper case.
> For example, rewrite the file with the word "organizationsController"
> changed to "OrganizationsController".
> Thanks.
>
> Here's a sample file:
>
> #
> # A skeletal controller
> #
> class organizationsController < ApplicationController
> active_scaffold :organization
> # layout "activescaffold"
> end
>
>
10:02:47 [Temp]: cat x
#
# A skeletal controller
#
class organizationsController < ApplicationController
active_scaffold :organization
# layout "activescaffold"
end

10:03:46 [Temp]: ruby -i.bak -pe 'gsub(/\bclass\s+(\w+)\b/) {"class " <<
$1.capitalize}' x
10:04:25 [Temp]: cat x
#
# A skeletal controller
#
class Organizationscontroller < ApplicationController
active_scaffold :organization
# layout "activescaffold"
end


10:04:27 [Temp]:

Kind regards

robert