我有字符串gitproject
如何使用带有gitproject 参数的 sed 命令来执行以下操作:
http://user_name:user_password@example.com/gitproject.git
http://example.com/gitproject.git
BR
您可以使用${VARIABLE_NAME}
语法来扩展 shell 变量,而无需在任一侧添加任何额外的空格,例如:
proj=gitproject
echo http://user_name_userpassword@example.com/${proj}.git
echo http://example.com/${proj}.git
不需要 sed:
str="gitproject"
echo "http://user_name:user_password@example.com/${str}.git"
echo "http://example.com/${str}.git"
如被问及,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