Using sed to replace between a specific pattern

It is possible with GNU sed. Choose one of these two forms based on the greediness of the replacement.

sed 's|=.*,|\L&|' file
sed 's|=[^,]*,|\L&|' file

As the manual states, "\L turns the replacement to lowercase until a \U or \E is found". & is the text matched by the regex.


I have modified the sample file to show that you should wisely choose between the geedy =.*, and the non-greedy =[^,]*, regexes:

$ cat file
SOMENAME=WOODSTOCK,
SOMEOTHERNAME2=JIMMY,WOODSTOCK,FINISH
$ sed 's|=.*,|\L&|' file
SOMENAME=woodstock,
SOMEOTHERNAME2=jimmy,woodstock,FINISH
$ sed 's|=[^,]*,|\L&|' file
SOMENAME=woodstock,
SOMEOTHERNAME2=jimmy,WOODSTOCK,FINISH

On any POSIX compliant awk, you can use the tolower() function

awk -v FS='=' 'BEGIN { OFS = FS } { $2 = tolower($2) }1 ' file

Use the mktemp() function to create a temp file to redirect the output of above command and then re-direct back to save in-place. Or use GNU awk >= 4.1 with -i inplace

Another variant of POSIX compliant version, using match() function would be to do

awk 'match($0, /=([^,]*),/) { str = substr($0, RSTART+1, RLENGTH-2); sub(str, tolower(str)) }1'