shd
shd

Reputation: 1119

How to replace lower case with sed

SET_VALUE(ab.ms.r.gms_dil_cfg.f().gms_dil_mode, dsad_sd );

How can I use sed to replace only from the SET_VALUE until the , with each letter after _ to be upper case?

result:

SET_VALUE(ab.ms.r.gmsDilCfg.f().gmsDilMode, dsad_sd );

Upvotes: 1

Views: 163

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

For your input string you may apply the following sed expression + bash variable substitution:

s="SET_VALUE(ab.ms.r.gms_dil_cfg.f().gms_dil_mode, dsad sd )"
res=$(sed '1s/_\([a-z]\)/\U\1/g;' <<< "${s%,*}"),${s#*,}
echo "$res"

The output:

SET_VALUE(ab.ms.r.gmsDilCfg.f().gmsDilMode, dsad_sd );

Upvotes: 2

markp-fuso
markp-fuso

Reputation: 34334

Got distracted while writing this one up so Roman beat me to the punch, but this has a slight variation so figured I'd post it as another option ...

$ s="SET_VALUE(ab.ms.r.gms_dil_cfg.f().gms_dil_mode, dsad_sd );"
$ sed 's/,/,\n/g' <<< "$s" | sed -n '1{s/_\([a-z]\)/\U\1/g;N;s/\n//;p}'    
SET_VALUE(ab.ms.r.gmsDilCfg.f().gmsDilMode, dsad_sd );
  • s/,/,\n/g : break input into separate lines at the comma (leave comma on first line, push rest of input to a second line)
  • at this point we've broken our input into 2 lines; the second sed invocation will now be working with a 2-line input
  • sed -n : refrain from printing input lines as they're processed; we'll explicitly print lines when required
  • 1{...} : for the first line, apply the commands inside the braces ...
  • s/_\([a-z]\)/\U\1/g : for each pattern we find like '_[a-z]', save the [a-z] in buffer #1, and replace the pattern with the upper case of the contents of buffer #1
  • at this point we've made the desired edits to line #1 (ie, everything before the comma in the original input), now ...
  • N : read and append the next line into the pattern space
  • s/\n// : replace the carriage return with a null character
  • at this point we've pasted lines #1 and #2 together into a single line
  • p : print the pattern space

Upvotes: 0

Related Questions