2

I'd like to replace the space () 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. 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 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.

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've been hesitant to tag this with sed as I'm not having the issue with sed but with replacing the string. If sed is the proper tool for the job, that's great! If it's something else, that's great too! – Alexandre Vaillancourt Jul 20 '18 at 16:58
1

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.

  • I'm still interested in finding a more elegant way to achieve this, if any. – Alexandre Vaillancourt Jul 20 '18 at 18:09
  • You don't need the pattern part of /prefix/ s/prefixmore/result/ -- if you run the s command on all lines, it will simply leave unchanged the lines that don't have /prefix/ – dave_thompson_085 Jul 23 '18 at 16:41
  • Ok, thanks for your comment, I'll try that later tonight! – Alexandre Vaillancourt Jul 23 '18 at 16:43
0
 sed '/^Node-path: branches\/ /s/ /_/2' < sedtest

look the space between the two slashes after branches

  • I'm afraid this does not work. This will fix the branches that begin with a space, such as branches/ branch0-blabla_blabla/src, but will not fix the branch names that have a space in the middle such as branches/branch1 stuffthing/src/lib 0. – Alexandre Vaillancourt Jul 20 '18 at 17:19
0

Perl lookbehind makes it a little simpler, though I wouldn't claim much:

perl -ple 's:^Node-path: branches/\K([^ /]*) :$1_:' <file

or maybe awk, which can handle more than one space in the branchname by changing to gsub:

awk -F/ -vOFS=/ '$1=="Node-path: branches"{sub(/ /,"_",$2)} 1' <file

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.