0

Globally in a file, I want to find all occurrences of strings starting with .git#v and replace the part of the string from the v until the next " with another string.

So, f.e. if I had migrations.git#v1.1.1", - I want to find it by .git#v and replace v1.1.1 with develop. The point is that the version number will vary for the different occurrences, that's why I need to rely on finding these pieces by the .git#v part.

I've got this so far:

sed -i -- 's/.git#v/.git#develop/g' package.json

but I know this is quite not enough because it will only replace the whole .git#v with develop.

1 Answer 1

2
echo 'migrations.git#v1.1.1"' |sed -r 's/\.git#v[^"]+"/\.git#develop/g'
migrations.git#develop

IF you do not wish to use -r : + sign needs to be taken care of.

sed 's/\.git#v[^"]\+"/\.git#develop/g'
migrations.git#develop

awk solution , using gsub :

echo 'migrations.git#v1.1.1"' |awk '{gsub(/.git#v[^"]+"/,".git#develop")}1'
migrations.git#develop
Sign up to request clarification or add additional context in comments.

8 Comments

Oh, forgot to say I'm on OSX. -r is an illegal option for sed.
@Milkncookiez check update in answer for alternate approch.
I'd use sed 's/\.git#v[^"]\+"/\.git#develop/g' package.json but it would only print the contents of the file, w/o any actions performed on it.
did you tried -E option like sed -E 's/\.git#v[^"]+"/\.git#develop/g' ?
@Milkncookiez echo 'migrations.git#v1.1.1"' | sed 's/\.git#v[^"]*/.git#develop/g' should work with any sed version...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.