bash - Replace [a-z],[a-z] with [a-z], [a-z] and keep the letters -


how can replace [a-z],[a-z] [a-z], [a-z] , keeping letters?

input

suny stony brook, stony brook,usa. 

output

suny stony brook, stony brook, usa. 

what have tried

sed 's/[a-z],[a-z]/[a-z], [a-z]/g' <<< "suny stony brook, stony brook,usa." sed 's/[a-z],[a-z]/, /g' <<< "suny stony brook, stony brook,usa." 

what have tried

sed 's/[a-z],[a-z]/[a-z], [a-z]/g' <<< "suny stony brook, stony brook,usa." 

you need use regex's capture groups here refer original [a-z] values.

for example:

s/\([a-z]\),\([a-z]\)/\1, \2/g 

notice how i've surrounded [a-z] \( , \)? these form capture groups can later referenced writing \1, \2, etc. (the number indicates position.)

alternatively, enable extended regexes specifying -r switch in sed (e.g. sed -r), in case need write ( , ) form capture groups.

putting together

sed -re 's/([a-z]),([a-z])/\1, \2/g' <<<"suny stony brook, stony brook,usa." 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -