Quick sed replacemnt

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

On Wed, 2007-06-13 at 04:14 +0900, yitzhakbg wrote:

A skeletal controller

class organizationsController < ApplicationController
active_scaffold :organization

layout “activescaffold”

end

sed ‘s/organizationsController/OrganizationsController/’ file > newfile

On Wed, 2007-06-13 at 04:14 +0900, yitzhakbg wrote:

A skeletal controller

class organizationsController < ApplicationController
active_scaffold :organization

layout “activescaffold”

end

perl -pi -e ‘s/organizationsController/OrganizationsController/’ file

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, 1:26 pm, yitzhakbg [email protected] 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”

On 6/12/07, yitzhakbg [email protected] 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 :wink:

Cheers,
Peter C.

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

On 6/12/07, Peter C. [email protected] wrote:

On 6/12/07, yitzhakbg [email protected] 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 :slight_smile:

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

Cheers,
Peter C.

Hey, that’s what I was looking for! I’ll try it and get back to you.
Thanks

2007/6/12, Felix W. [email protected]:

ruby -e ‘puts $_.gsub(/(class )(.)/) { “#{$1}#{$2.upcase}” }’ -n
[filename]

very dirty

Very nice. Thanks indeed!

2007/6/12, Felix W. [email protected]:

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

Hi,

Am Mittwoch, 13. Jun 2007, 07:20:01 +0900 schrieb Bertram S.:

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

On 12.06.2007 21:14, yitzhakbg wrote:

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