Quick sed replacemnt

Y

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".
Thanks.

Here's a sample file:

#
# A skeletal controller
#
class organizationsController < ApplicationController
active_scaffold :eek:rganization
# layout "activescaffold"
end
 
R

Reid Thompson

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 :eek:rganization
# layout "activescaffold"
end

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

Reid Thompson

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 :eek:rganization
# layout "activescaffold"
end
perl -pi -e 's/organizationsController/OrganizationsController/' file
 
Y

yitzhakbg

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?
 
P

Phrogz

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"
 
Y

yitzhakbg

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

Peter Cooper

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.rubyinside.com/
 
P

Peter Cooper

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.rubyinside.com/
 
B

Bertram Scharpf

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
 
B

Bertram Scharpf

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
 
R

Robert Klemme

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 :eek:rganization
# layout "activescaffold"
end
10:02:47 [Temp]: cat x
#
# A skeletal controller
#
class organizationsController < ApplicationController
active_scaffold :eek:rganization
# 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 :eek:rganization
# layout "activescaffold"
end


10:04:27 [Temp]:

Kind regards

robert
 

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
474,261
Messages
2,571,308
Members
47,967
Latest member
pakasi

Latest Threads

Top