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_Name
→ getEmployeeName
us_employee_name
→ usEmployeeName
And what if we had id
and we wanted to capitalize both I
and D
, as in
us_employee_id
→ usEmployeeID
?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?
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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With