sed: using part of the regular expression to parse the text
I have a line of text which looks like this:
/src/my_module_my_branch/my_module
Example: /src/goodcode_dev/goodcode
and I need to convert it to:
开发者_Python百科/src/my_branch/my_module
Example: /src/dev/goodcode
The problem both module and branch can include underscores. So I need first to identify the module from the third part and use it to extract out the branch name. Is there a way using sed to do such conversions?
If you have GNU sed, this should work. It assumes everything begins with /src/
.
sed -r 's/^\/src\/([^/]+)_([^/]+)\/\1$/\/src\/\2\/\1/'
In English:
Invoke sed
with extended regex (-r
) so that +
will work. Match beginning of line, /src/
, group 1: one or more non-slashes, _
, group 2: one or more non-slashes, /
, group 1, end of line. Change to /src/
, group 2, /
, group 1.
First, find the last part, then use it to substitute
$ echo "/src/my_module_my_branch/my_module" | awk -F"/" '{n=$NF;gsub(n"_","",$3);print}' OFS="/"
/src/my_branch/my_module
If there isn't any difference between the "module" and "branch" parts, then I'd say no. That is, if my_module_my
could be the module, and branch
the branch, then how would sed
know?
精彩评论