1

In requirements.txt, I want to replace the branch/commit which can contain forward slashes, dashes, and other potentially special characters that may need escaping:

-e git@github.com:acme-inc/repo0.git@master#egg=repo0
-e git@github.com:acme-inc/repo1.git@master#egg=repo1
-e git@github.com:acme-inc/repo2.git@master#egg=repo2

An example goal is to replace the content between @ and # with an arbitrary branch or commit. For example:

-e git@github.com:acme-inc/repo0.git@my/branch/0#egg=repo0
-e git@github.com:acme-inc/repo1.git@1234567#egg=repo1
-e git@github.com:acme-inc/repo2.git@my/branch-2#egg=repo2

This is NOT producing the desired goal's 0th row:

sed -i 's/(repo0.git@).*(#)/"my/branch/0"/' testfile.txt

Related:

http://www.grymoire.com/Unix/Sed.html#uh-62h

https://stackoverflow.com/questions/10613643/replace-a-unknown-string-between-two-known-strings-with-sed

Sed replace between 2 strings with special character

  • Your command references repo0 yet your example replaces on the repo1 line. – Steven Oct 27 '15 at 21:12
  • Also, why is the 1234567 in your example? – Steven Oct 27 '15 at 21:21
  • those are the two ways to specify the point to check out: the branch name or a specific commit hash which is often cut to just 7 characters. – tarabyte Oct 27 '15 at 21:23
  • it's more of my lack of knowledge around the common regex to use for this scenario and escaping issues. – tarabyte Oct 27 '15 at 21:23
  • Your question is still somewhat unclear. What specifically do you want answered? – Steven Oct 27 '15 at 21:40
2

To replace the characters between @ and # with my/branch, use the following:

$ sed -e 's!@[^@]*#!@my/branch#!' foo.txt
-e git@github.com:acme-inc/repo0.git@my/branch#egg=repo0
-e git@github.com:acme-inc/repo1.git@my/branch#egg=repo1
-e git@github.com:acme-inc/repo2.git@my/branch#egg=repo2

Note: the [^@]* ensures you match on the nearest @ and not the first.


Since your exact needs are unclear, I'll assume you want to swap master with my\branch.

Substitutions in sed are delimited by the first character after s. Using a character that doesn't occur in your string (such as !) might be easier to follow.

$ sed -e 's!master!my/branch!' foo.txt
-e git@github.com:acme-inc/repo0.git@my/branch#egg=repo0
-e git@github.com:acme-inc/repo1.git@my/branch#egg=repo1
-e git@github.com:acme-inc/repo2.git@my/branch#egg=repo2

Alternatively, you can escape the forward slash \/ so that sed won't try to delimit on it.

$ sed -e 's/master/my\/branch/' foo.txt
-e git@github.com:acme-inc/repo0.git@my/branch#egg=repo0
-e git@github.com:acme-inc/repo1.git@my/branch#egg=repo1
-e git@github.com:acme-inc/repo2.git@my/branch#egg=repo2
  • Not all the branches will be the same. I want to update a single branch of a single repo. I need to understand how to handle ., @ and # in the regex. – tarabyte Oct 27 '15 at 21:34
  • When matching for a literal period, escape it \.. The characters @ and # are not used in regular expressions and can be used just like letters and numbers. – Steven Oct 27 '15 at 21:38
  • @tarabyte See edited post for substituting @whatever# with @my/branch#. – Steven Oct 27 '15 at 21:51

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.