简体   繁体   中英

Bash script: find and replace uppercase character on a string

Saying we have a string like:

doSomething()

and we want to obtain:

do_something()

What is the best way to do this?
I've read documentation about strings manipulation but I can't find the right command combination..

update

After @anubhava discussion, I find solution installing gnu-sed:

brew install gnu-sed

And then I can run script in this way:

s="doSomethingElse()"; gsed 's/[[:upper:]]/_\L&/g' <<< "$s"

output: do_something_else()

Using gnu-sed you can do:

s='doSomethingElse()'

sed 's/[[:upper:]]/_\L&/g' <<< "$s"
do_something_else()

Or else with non-gnu-sed (BSD) pipe with tr :

sed 's/[[:upper:]]/_&/g' <<< "$s" | tr [[:upper:]] [[:lower:]]
do_something_else()

Or using perl :

perl -pe 's/[[:upper:]]/_\L$&/g' <<< "$s"
do_something_else()

Or using gnu-awk :

awk -v RS=[[:upper:]] -v ORS= '1; RT{printf "_%s", tolower(RT)}' <<< "$s"
do_something_else()

In bash 4:

$ s="doSomethingElse()"
$ while [[ $s =~ [[:lower:]]([[:upper:]]) ]]; do   
>     s=${s/[[:upper:]]/_${BASH_REMATCH[1],,}}
> done
$ echo "$s"
do_something_else()

First, the while loop tries to match a lowercase character immediately followed by an uppercase character, and capturing the matched uppercase character. The parameter expansion replaces the first uppercase character in the string with an underscore and the captured uppercase character (converted to lowercase by the ,, operator). The processes repeats until no more lower/upper pairs are found.


If bash allowed capture groups in patterns, something hypothetical like

s=${s//([[:lower:]])([[:upper:]])/${BASH_PATMATCH[1]}_${BASH_PATMATCH[2],,}}

could work without a loop. As is, we need the extra step of using regular expression matches one match at a time to capture the letter to be lowercased.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM