我想转换包含一堆问题 svn URL 的列表:
cat list.txt
//svn.server.address/repos/project/module1/branches/issue-001-name1
//svn.server.address/repos/project/module2/branches/issue-002-name2
//svn.server.address/repos/project/module3/branches/issue-003-name3
...
进入 newlist.txt 加上 svn 命令和文件夹名称来签出它们,如下所示:
svn co //svn.server.address/repos/project/module1/branches/issue-001-name1 issue-001-module1
svn co //svn.server.address/repos/project/module2/branches/issue-002-name2 issue-002-module2
svn co //svn.server.address/repos/project/module3/branches/issue-003-name3 issue-003-module3
...
我尝试过类似的事情:
eval $(awk -F'/branches/' '{print $2}' list.txt|awk -F'-' '{print "i="$1"-"$2}')
eval $(awk -F'/branches/' '{print $1}' list.txt|awk -F'/' '{print "j="$NF}')
name=$i"-"$j
awk '{print "svn co "$1" "n}' n=$name list.txt >newlist.txt
但它总是会得到最后一个变量。
答案1
awk v1
看来您想将第三个路径元素与最后一个路径元素结合使用作为要签出的模块。在这种情况下,你可以在 awk 中这样做:
awk -F/ '
{
split($NF, a, "-")
module=a[1] "-" a[2] "-" $6
print "svn co " $0 " " module
}' list.txt
解释
split
$NF
用连字符将数组分隔开a
(例如a[1] = issue
,a[2] = 001
和a[3] = name1
)。然后,前两个元素a
与第三个路径元素 ( ) 连接在一起$6
以生成所需的模块名称。
awk v2
如果您的 awk 版本支持将字段分隔符指定为正则表达式,则可以使用以下更简单的替代方法:
awk -F'[/-]' '{ print "svn co " $0 " " $8 "-" $9 "-" $6 }' list.txt
sed
GNU sed 替代方案:
sed -r 's:(([^/]*/){5})([^/]+)/([^/]+)/(([^-]+-){2})(.*):\1\3/\4/\5\7 \5\3:'
解释
(([^/]*/){5})
前五个路径元素,注释//
计数为两个元素。([^/]+)
模块位。([^/]+)
分支位。(([^-]+-){2})
前两个连字符分隔的元素,例如issue-001-
.
所有情况下的输出
svn co //svn.server.address/repos/project/module1/branches/issue-001-name1 issue-001-module1
svn co //svn.server.address/repos/project/module2/branches/issue-002-name2 issue-002-module2
svn co //svn.server.address/repos/project/module3/branches/issue-003-name3 issue-003-module3
答案2
这也可以通过以下方式轻松完成sed
:
$ sed 's|\(.*/\(.*\)\)|svn co \1 \2|g' file
svn co //svn.server.address/repos/project/module1/branches/issue-001-name1 issue-001-name1
svn co //svn.server.address/repos/project/module2/branches/issue-002-name2 issue-002-name2
svn co //svn.server.address/repos/project/module3/branches/issue-003-name3 issue-003-name3
答案3
您可以尝试执行以下命令吗?
cat list.txt |
awk -F'/branches/' '{a=gensub(/name/,"module", 1, $2); print "svn co "$0" " a}'
答案4
$cat d.txt //svn.server.address/repos/project/module1/branches/issue-001-name1 //svn.server.address/repos/project/module2/branches/issue-002-name2 //svn.server.address/repos/project/module3/branches/issue-003-name3 $awk 'BEGIN{FS="/"}{print "svn","co",$0,$8}' d.txt svn co //svn.server.address/repos/project/module1/branches/issue-001-name1 issue-001-name1 svn co //svn.server.address/repos/project/module2/branches/issue-002-name2 issue-002-name2 svn co //svn.server.address/repos/project/module3/branches/issue-003-name3 issue-003-name3