从字符串制作网址 - sed?

时间:2013-10-04 14:47:55

标签: bash sed

我有字符串 gitproject

如何在 gitproject参数中使用sed命令:

  • http://user_name:user_password@example.com/gitproject.git
  • http://example.com/gitproject.git

BR

3 个答案:

答案 0 :(得分:2)

您可以使用${VARIABLE_NAME} syntax扩展shell变量,而不会在任何一侧添加任何额外的空格,例如:

proj=gitproject
echo http://user_name_userpassword@example.com/${proj}.git
echo http://example.com/${proj}.git

答案 1 :(得分:2)

不需要sed:

str="gitproject"
echo "http://user_name:user_password@example.com/${str}.git"
echo "http://example.com/${str}.git"

答案 2 :(得分:0)

按照要求,sed方式:

echo "http://user_name:user_password@example.com/gitproject.git" |
    sed -e 's|//[^/]*/|//example.com/|'
http://example.com/gitproject.git

现在只有bash

gitproject="http://user_name:user_password@example.com/gitproject.git"
proto=${gitproject%%//*}
path=${gitproject#${proto}//*/}
echo ${proto}//example.com/$path
http://example.com/gitproject.git