1

I've inherited a bunch of C++ files that need to have function and variable names changed to meet our new C++ coding 'standards'.

Like all C/C++ code, there are variables/functions such as my_new_function or My_Newer_Function...

The Java folks have forced a camel cap style on us, so what I want to do is search for any underscore and make the next letter capitalized and have the underscore removed, that is:

my_new_function becomes myNewFunction

and

My_Newer_Function becomes MyNewerFunction

also, if the name has a number in it such as my_8th, it just removes the '_' to become my8th. This should probably be a separate regex.

I have some general knowledge of regex but this one has stumped me.. and with so many files and so little time, I have come to the beneficent gathering of the members of SO for help.

Thank you in advance.

Yes, I know, I should make the Java folks do this, but I just work here...

;-)

4

2 回答 2

0

You can do:

sed -r 's/_([a-z])/\U\1/g' file

See it

于 2013-04-17T17:13:13.657 回答
0

In my experience, if you try to change your code massively with a single command you risk to find exceptions (e.g., quoted text).

I suggest you to build a shell:

1. for each file
2.    get the names of variables/functions 
3.    for each name apply the sed command to get its new-name
4.       on the fly build a sed command to replace exactly each name with the new-name in the the file

…costly but secure…</p>

于 2013-04-17T18:53:57.270 回答