I'm trying to convert this

url = ssh://git@git.company.com/project/repo.git


url = ssh://git@git.company.com/scm/project/repo.git

However, it's not a simple sed find and replace. I'd like it to only apply the find and replace if the string starts with \turl and doesn't already contain scm.

I have the beginning portion where it will only replace lines containing \turl

cat .git/config | sed "/\turl/ s=\.com\/=\.com\/scm\/=g"

but to make this idempotent, I'm having trouble checking if it contains the scm string already. Otherwise if I keep running the command with seds -i then it will keep adding scm.


In sed, this should work:

sed '/url/ { /scm/! s=\.com/=\.com/scm/=g }'

You can negate an andress by adding a ! after it, and you can apply an address regex to another by using blocks.

Or maybe, if scm only afer .com/ should be checked for:

sed '/url/ { \=\.com/scm/= ! s=\.com/=\.com/scm/=g }'
! grep -q '/scm/' .git/config && sed "/\turl/ s=\.com\/=\.com\/scm\/=g" .git/config


grep -q '/scm/' .git/config || sed "/\turl/ s=\.com\/=\.com\/scm\/=g" .git/config

If the file doesn't contain /scm/, then sed ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.