Quick sed replacemnt


#1

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


#2

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


#3

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


#4

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?


#5

On Jun 12, 1:26 pm, yitzhakbg removed_email_address@domain.invalid 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”


#6

On 6/12/07, yitzhakbg removed_email_address@domain.invalid 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.
http://www.rubyinside.com/


#7

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


#8

On 6/12/07, Peter C. removed_email_address@domain.invalid wrote:

On 6/12/07, yitzhakbg removed_email_address@domain.invalid 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.
http://www.rubyinside.com/


#9

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

2007/6/12, Felix W. removed_email_address@domain.invalid:


#10

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

very dirty


#11

Very nice. Thanks indeed!

2007/6/12, Felix W. removed_email_address@domain.invalid:


#12

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


#13

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


#14

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