Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to convert a list containing bunch of svn URLs of issues:

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
    ...

into newlist.txt that plus svn command and folder names to checkout them, like this:

    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
    ...

I have tried something like:

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

But it would always get the last variable, I'm totally newbie for this so any help would be greatly appreciated!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

awk v1

It seems you want to use the 3rd path element combined with last path element as the module to check out. In that case you can do it like this within awk:

awk -F/ '
{
  split($NF, a, "-")
  module=a[1] "-" a[2] "-" $6
  print "svn co " $0 " " module
}' list.txt

Explanation

split separates $NF at hyphens into the a array (e.g. a[1] = issue, a[2] = 001 and a[3] = name1). The first two elements of a are then concatenated together with the 3rd path element ($6) to produce the desired module name.

awk v2

If your version of awk supports specifying the field separator as a regular expression, you can use this simpler alternative:

awk -F'[/-]' '{ print "svn co " $0 " " $8 "-" $9 "-" $6 }' list.txt

sed

A GNU sed alternative:

sed -r 's:(([^/]*/){5})([^/]+)/([^/]+)/(([^-]+-){2})(.*):\1\3/\4/\5\7 \5\3:'

Explanation

  • (([^/]*/){5}) first five path elements, note // counts for two elements.
  • ([^/]+) the module bit.
  • ([^/]+) the branch bit.
  • (([^-]+-){2}) first two hyphen delimited elements, e.g. issue-001-.

Output in all cases

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
share|improve this answer
    
Thanks man! I think v1 is a good approach for my current situation, I will do some modification to let it fit.:) –  mr.chen Jan 29 '13 at 9:48

This can also be easily accomplished with 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
share|improve this answer
    
yes, I also think sed can handle this, thanks=:) –  mr.chen Jan 29 '13 at 8:45

Can you try following command?

cat list.txt |
awk -F'/branches/' '{a=gensub(/name/,"module", 1, $2); print "svn co  "$0" " a}'
share|improve this answer
    
Yes, but actually the "module" can be any names. Thanks anyway! –  mr.chen Jan 29 '13 at 9:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.