-1

我有字符串gitproject

如何使用带有gitproject 参数的 sed 命令来执行以下操作:

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

BR

4

3 回答 3

2

您可以使用${VARIABLE_NAME}语法来扩展 shell 变量,而无需在任一侧添加任何额外的空格,例如:

proj=gitproject
echo http://user_name_userpassword@example.com/${proj}.git
echo http://example.com/${proj}.git
于 2013-10-04T14:50:50.083 回答
2

不需要 sed:

str="gitproject"
echo "http://user_name:user_password@example.com/${str}.git"
echo "http://example.com/${str}.git"
于 2013-10-04T14:50:54.557 回答
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
于 2013-10-04T15:55:36.797 回答