Forum: Ruby Quick sed replacemnt

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ea94dbb5d740e9d4adc6200f9c563452?d=identicon&s=25 Yitzhak Bar Geva (yitzhakbg)
on 2007-06-12 21:15
(Received via mailing list)
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
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-12 21:21
(Received via mailing list)
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
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-12 21:23
(Received via mailing list)
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
Ea94dbb5d740e9d4adc6200f9c563452?d=identicon&s=25 Yitzhak Bar Geva (yitzhakbg)
on 2007-06-12 21:27
(Received via mailing list)
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?
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-06-12 21:36
(Received via mailing list)
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"
Ea94dbb5d740e9d4adc6200f9c563452?d=identicon&s=25 Yitzhak Bar Geva (yitzhakbg)
on 2007-06-12 21:38
(Received via mailing list)
Thank you, but you still didn't get it. organizationscontroller is
only an example. It could be anything else
509e15b158eaac5ab3a5332a3f89d4a7?d=identicon&s=25 Peter Cooper (Guest)
on 2007-06-12 21:39
(Received via mailing list)
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.rubyinside.com/
509e15b158eaac5ab3a5332a3f89d4a7?d=identicon&s=25 Peter Cooper (Guest)
on 2007-06-12 21:40
(Received via mailing list)
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.rubyinside.com/
15a5043475dac9278ae75efb4c71f1f6?d=identicon&s=25 Felix Windt (Guest)
on 2007-06-12 21:56
(Received via mailing list)
ruby -e 'puts $_.gsub(/(class )(.)/) { "#{$1}#{$2.upcase}" }' -n
[filename]

very dirty
Ea94dbb5d740e9d4adc6200f9c563452?d=identicon&s=25 Yitzhak Bar Geva (yitzhakbg)
on 2007-06-12 22:56
(Received via mailing list)
Hey, that's what I was looking for! I'll try it and get back to you.
Thanks

2007/6/12, Felix Windt <fwmailinglists@gmail.com>:
Ea94dbb5d740e9d4adc6200f9c563452?d=identicon&s=25 Yitzhak Bar Geva (yitzhakbg)
on 2007-06-12 23:32
(Received via mailing list)
Very nice. Thanks indeed!

2007/6/12, Felix Windt <fwmailinglists@gmail.com>:
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-06-13 00:21
(Received via mailing list)
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
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-06-13 09:08
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-13 10:11
(Received via mailing list)
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
This topic is locked and can not be replied to.