Sed reqular expression

Sed reqular expression

am 09.11.2007 15:40:05 von schouwla

How do I rename a lot of source code using sed?

before foo_test_1(var_test t)
after FooTest1(varTest t)

or if that is to diffcult
FooTest1(VarTest t)

Cheers
Lars Schouw

Re: Sed reqular expression

am 09.11.2007 17:13:07 von Glenn Jackman

At 2007-11-09 09:40AM, "Lars Schouw" wrote:
> How do I rename a lot of source code using sed?
>
> before foo_test_1(var_test t)
> after FooTest1(varTest t)
>
> or if that is to diffcult
> FooTest1(VarTest t)

1. To remove the underscores:
sed -e 's/_\([[:alnum:]]\)/\U\1/g'

What's your algorithm to know when to uppercase the first letter? What
would you do in these cases:
foo_test_1(var_test t)
x = foo_test_1(var_test t)
x=foo_test_1(var_test t)
f(foo_test_1(var_test t))

2. To uppercase the first letter of any word containing an underscore:
sed -e 's/\<\([[:lower:]]\)\([[:alnum:]]*_\)/\U\1\E\2/g'

To uppercase the first letter of a word containing an underscore if the
word starts the line or is preceded by whitespace:
sed -e 's/\(^\|[[:blank:]]\)\([[:lower:]]\)\([[:alnum:]]*_\)/\1\U\2 \E\3/g'

If you're going to use both sed scripts, use #2 first, because it relies
on underscores being present, and #1 removes underscores.

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: Sed reqular expression

am 09.11.2007 21:41:41 von Ed Morton

Lars Schouw wrote:
> How do I rename a lot of source code using sed?
>
> before foo_test_1(var_test t)
> after FooTest1(varTest t)
>
> or if that is to diffcult
> FooTest1(VarTest t)
>
> Cheers
> Lars Schouw
>

You're severely restricting the responses you get by insisting on a
"sed" solution, so if that isn't really a requirement, let us know.

Ed.

Re: Sed reqular expression

am 10.11.2007 12:57:16 von schouwla

Ed,
No, any solution is fine.. OS also fee Unix or Windows.
Do you have something smart?

Lars
>
> You're severely restricting the responses you get by insisting on a
> "sed" solution, so if that isn't really a requirement, let us know.
>
> Ed.

Re: Sed reqular expression

am 10.11.2007 13:00:10 von schouwla

Glenn,

I was playing around with this sed script but it will not do uppercase
on the first letter in the work. I can lieve with that since I don't
know how to seperate functions (capital first letter) from variable
(lower captial first letter).

cat $1 |
sed 's/_a/A/g' |
sed 's/_b/B/g' |
sed 's/_c/C/g' |
sed 's/_d/D/g' |
sed 's/_e/E/g' |
sed 's/_f/F/g' |
sed 's/_g/G/g' |
sed 's/_h/H/g' |
sed 's/_i/I/g' |
sed 's/_j/J/g' |
sed 's/_k/K/g' |
sed 's/_l/L/g' |
sed 's/_m/M/g' |
sed 's/_n/N/g' |
sed 's/_o/O/g' |
sed 's/_p/P/g' |
sed 's/_q/Q/g' |
sed 's/_r/R/g' |
sed 's/_s/S/g' |
sed 's/_t/T/g' |
sed 's/_u/U/g' |
sed 's/_v/V/g' |
sed 's/_w/W/g' |
sed 's/_x/X/g' |
sed 's/_y/Y/g' |
sed 's/_z/Z/g' |
sed 's/_1/1/g' |
sed 's/_2/2/g' |
sed 's/_3/3/g' |
sed 's/_4/4/g' |
sed 's/_5/5/g' |
sed 's/_6/6/g' |
sed 's/_7/7/g' |
sed 's/_8/8/g' |
sed 's/_9/9/g'

I am not 100% happy with this so still looking for better approaces.

Lars
On Nov 10, 1:10 am, Glenn Jackman wrote:
> At 2007-11-09 09:40AM, "Lars Schouw" wrote:
>
> > How do I rename a lot of source code using sed?
>
> > before foo_test_1(var_test t)
> > after FooTest1(varTest t)
>
> > or if that is to diffcult
> > FooTest1(VarTest t)
>
> 1. To remove the underscores:
> sed -e 's/_\([[:alnum:]]\)/\U\1/g'
>
> What's your algorithm to know when to uppercase the first letter? What
> would you do in these cases:
> foo_test_1(var_test t)
> x = foo_test_1(var_test t)
> x=foo_test_1(var_test t)
> f(foo_test_1(var_test t))
>
> 2. This piece of sed will uppercase the first letter of a word containing
> an underscore if the word starts the line or is preceded by whitespace:
> sed -e 's/\(^\|[[:blank:]]\)\([[:lower:]]\)\([[:alnum:]]*_\)/\1\U\2 \E\3/g'
>
> If you're going to use both sed scripts, use #2 first, because it relies
> on underscores being present, and #1 removes underscores.
>
> --
> Glenn Jackman
> "You can only be young once. But you can always be immature." -- Dave Barry

Re: Sed reqular expression

am 10.11.2007 17:56:11 von Ed Morton

On 11/10/2007 5:57 AM, Lars Schouw wrote:

[ please don't top-post and please provide enough context for your post to
stand-alone, fixed below ]

> Lars Schouw wrote:
>
>> How do I rename a lot of source code using sed?
>>
>> before foo_test_1(var_test t)
>> after FooTest1(varTest t)
>>
>> or if that is to diffcult
>> FooTest1(VarTest t)
>
> You're severely restricting the responses you get by insisting on a
> "sed" solution, so if that isn't really a requirement, let us know.
>
> Ed,
> No, any solution is fine.. OS also fee Unix or Windows.
> Do you have something smart?

It depends what your input file looks like. You showed us what you want to do
with that symbol, but without knowing what context it appears in in your input
file, it's hard to know what type of solution will work without messing up the
rest of your file. Show a small, real, sample input and expected output.

Ed.

Re: Sed reqular expression

am 12.11.2007 00:16:28 von schouwla

Ed,

Like this:

before:
typedef hnd X_Y;
bool is_a_X(const X_Y& inst) { return inst.isA(); }

after:
typedef hnd XY;
bool IsAZ(const XY& inst) { return inst.isA(); }

Lars


On Nov 11, 1:56 am, Ed Morton wrote:
> On 11/10/2007 5:57 AM, Lars Schouw wrote:
>
> [ please don't top-post and please provide enough context for your post to
> stand-alone, fixed below ]
>
>
>
>
>
> > Lars Schouw wrote:
>
> >> How do I rename a lot of source code using sed?
>
> >> before foo_test_1(var_test t)
> >> after FooTest1(varTest t)
>
> >> or if that is to diffcult
> >> FooTest1(VarTest t)
>
> > You're severely restricting the responses you get by insisting on a
> > "sed" solution, so if that isn't really a requirement, let us know.
>
> > Ed,
> > No, any solution is fine.. OS also fee Unix or Windows.
> > Do you have something smart?
>
> It depends what your input file looks like. You showed us what you want to do
> with that symbol, but without knowing what context it appears in in your input
> file, it's hard to know what type of solution will work without messing up the
> rest of your file. Show a small, real, sample input and expected output.
>
> Ed.- Hide quoted text -
>
> - Show quoted text -

Re: Sed reqular expression

am 12.11.2007 00:18:09 von schouwla

Ed,

Like this:

before:
typedef hnd X_Y;
bool is_a_X(const X_Y& inst) { return inst.isA(); }


after:
typedef hnd XY;
bool IsAZ(const XY& inst) { return inst.isA(); }

Lars

Re: Sed reqular expression

am 12.11.2007 00:18:50 von schouwla

Ed,

Like this:


before:
typedef hnd X_Y;
bool is_a_Z(const X_Y& inst) { return inst.isA(); }


after:
typedef hnd XY;
bool IsAZ(const XY& inst) { return inst.IsA(); }


Lars



On Nov 11, 1:56 am, Ed Morton wrote:
> On 11/10/2007 5:57 AM, Lars Schouw wrote:
>
> [ please don't top-post and please provide enough context for your post to
> stand-alone, fixed below ]
>
>
>
>
>
> > Lars Schouw wrote:
>
> >> How do I rename a lot of source code using sed?
>
> >> before foo_test_1(var_test t)
> >> after FooTest1(varTest t)
>
> >> or if that is to diffcult
> >> FooTest1(VarTest t)
>
> > You're severely restricting the responses you get by insisting on a
> > "sed" solution, so if that isn't really a requirement, let us know.
>
> > Ed,
> > No, any solution is fine.. OS also fee Unix or Windows.
> > Do you have something smart?
>
> It depends what your input file looks like. You showed us what you want to do
> with that symbol, but without knowing what context it appears in in your input
> file, it's hard to know what type of solution will work without messing up the
> rest of your file. Show a small, real, sample input and expected output.
>
> Ed.- Hide quoted text -
>
> - Show quoted text -

Re: Sed reqular expression

am 13.11.2007 00:53:48 von Ed Morton

On 11/11/2007 5:18 PM, Lars Schouw wrote:

[ final request - PLEASE stop top-posting! Fixed below, again ]

> On Nov 11, 1:56 am, Ed Morton wrote:
>
>>On 11/10/2007 5:57 AM, Lars Schouw wrote:
>>
>>[ please don't top-post and please provide enough context for your post to
>>stand-alone, fixed below ]
>>
>>
>>
>>
>>
>>
>>>Lars Schouw wrote:
>>
>>>>How do I rename a lot of source code using sed?
>>>
>>>>before foo_test_1(var_test t)
>>>>after FooTest1(varTest t)
>>>
>>>>or if that is to diffcult
>>>>FooTest1(VarTest t)
>>>
>>>You're severely restricting the responses you get by insisting on a
>>>"sed" solution, so if that isn't really a requirement, let us know.
>>
>>>Ed,
>>>No, any solution is fine.. OS also fee Unix or Windows.
>>>Do you have something smart?
>>
>>It depends what your input file looks like. You showed us what you want to do
>>with that symbol, but without knowing what context it appears in in your input
>>file, it's hard to know what type of solution will work without messing up the
>>rest of your file. Show a small, real, sample input and expected output.
>>
>> Ed.- Hide quoted text -
>>
>>- Show quoted text -
>
> Ed,
>
> Like this:
>
>
> before:
> typedef hnd X_Y;
> bool is_a_Z(const X_Y& inst) { return inst.isA(); }
>
>
> after:
> typedef hnd XY;
> bool IsAZ(const XY& inst) { return inst.IsA(); }
>

You'd indicated earlier that you had some flexibility in names:

> before foo_test_1(var_test t)
> after FooTest1(varTest t)
>
> or if that is to diffcult
> FooTest1(VarTest t)

If so, then maybe this solution is adequate:

$ cat file
typedef hnd X_Y;
bool is_a_Z(const X_Y& inst) { return inst.isA(); }

$ cat cvt.awk
BEGIN{FS=OFS=""}
{
p = ""
for (i=1;i<=NF;i++) {
if (p == "_")
$i = toupper($i)
p = $i
if ($i == "_")
$i = ""
}
}
1

$ awk -f cvt.awk file
typedef hnd XY;
bool isAZ(const XY& inst) { return inst.isA(); }

If not, it'd require a little different approach...

Regards,

Ed