Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove underscore and capitalize character after it

I was just wondering if there is a way to replace every underscore in every file in a folder (say .java files) and convert the next character to uppercase, like

  • getEmployee_NamegetEmployeeName
  • us_employee_nameusEmployeeName

And what if we had id and we wanted to capitalize both I and D, as in

  • us_employee_idusEmployeeID?

I haven't tried anything yet since I'm still learning. Can I do something like s/_/\U\1/g in sed or can I use some script to do this?

like image 414
gran_profaci Avatar asked Jun 26 '13 23:06

gran_profaci


2 Answers

Your suggestion 's/_/\U\1/g' is very close. If you have the GNU sed, then the following should work:

sed 's/_\(.\)/\U\1/g'

(I say should, because what you wish for is not always what you want.)

like image 145
Joseph Quinsey Avatar answered Sep 29 '22 14:09

Joseph Quinsey


Little more verbose from awk but it will work on all gnu/non-gnu Unix flavors:

> s='get_employee_Name'
> awk -F _ '{printf "%s", $1; for(i=2; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i, 2); print"";}' <<< "$s"
getEmployeeName
like image 35
anubhava Avatar answered Sep 29 '22 12:09

anubhava