Replace
如何替換 SVN 轉儲分支名稱中的空格字元?
我想替換空間(
) character(s) in the name of the branches with an underscore (
_). For example, say I have a *sedtest* file containing:
Node-path: trunk/ Node-path: trunk/src/lib 0 Node-path: trunk/src/lib 0/bla.txt Node-path: branches/ branch0-blabla_blabla/src Node-path: branches/ branch0-blabla_blabla Node-path: branches/branch1 stuffthing Node-path: branches/branch1 stuffthing/src/lib 0 Node-path: branches/branch1 stuffthing/src/lib 0/bla.txt Node-path: branches/branch2stuffthing/src/lib 0/bl a.txt ``` I'd like it to be modified like this: ``` Node-path: trunk/ Node-path: trunk/src/lib 0 Node-path: trunk/src/lib 0/bla.txt Node-path: branches/_branch0-blabla_blabla/src Node-path: branches/_branch0-blabla_blabla Node-path: branches/branch1_stuffthing Node-path: branches/branch1_stuffthing/src/lib 0 Node-path: branches/branch1_stuffthing/src/lib 0/bla.txt Node-path: branches/branch2stuffthing/src/lib 0/bl a.txt ``` I'm having an issue similar as the one described in [this SO question](https://stackoverflow.com/questions/33879823/use-regex-with-sed-to-replace-spaces-by-underscore). However, that question requests to replace all the spaces in the path. I'd like to replace the spaces only in the branches names. From that other question, I have modified the `sed` command to: ``` sed '/^Node-path: branches\//s/ /_/2' < sedtest ``` But it also modifies the last line to `Node-path: branches/branch2stuffthing/src/lib_0/bl a.txt`, which should be left untouched. My experience with `sed` being quite limited, I've been unable to properly limit the 'search region' to what is between `branches/` and the following `/` or `$`, whichever comes first. I'm using CentOS 7. As for what I'm trying to accomplish: I'm trying to port our SVN repository to a git one, and according to [this answer on SO](https://stackoverflow.com/a/3651867/637987) and on my personal experience, git branches names can't have spaces in them. I'm trying to fix this through a svn dump as suggested [on svnbook.red-bean.com](http://svnbook.red-bean.com/en/1.7/svn.reposadmin.maint.html#svn.reposadmin.maint.filtering). I suppose I could grep all the faulty branches and manually replace the strings, but that would be less generic, more work, and *much less entertaining*.`
I have built the following
sed` command:$ sed '/^Node-path: branches\//s/Node-path: branches\/\([-A-Za-z0-9]*\) \ \([-A-Za-z0-9]*\)/Node-path: branches\/\1_\2/' < sedtest
It looks big and ugly, but it works for this situation.
Note that it does not fix multiple occurrences of the space in the branch name, only one (so I will re-run the command if I need).
As a side note, if you’re doing this to fix an SVN dump, don’t forget to fix the
Node-copyfrom-path:
lines the same way.`