我有一个字符串:http://user_name:user_password@example.com/gitproject.git,并希望在没有user和pass - http://example.com/gitproject.git的情况下使用它
即
http://user_name:user_password@example.com/gitproject.git 至
http://example.com/gitproject.git如何在bash中自动执行此操作?
发布于 2013-10-04 21:56:49
您可能已经安装了一些语言,比如php或python,它们都有很好的URL解析工具。例如,php:
$url = parse_url("http://user_name:user_password@example.com/gitproject.git ");
return "$url[scheme]://" . $url['host'] . $url['path'];但是,由于这不是您所要求的,因此您仍然可以在sed中完成
sed -r "s#(.*?://).*?@(.*)#\1\2#" <<<"http://user:pass@example.com/git"发布于 2013-10-04 21:59:37
此sed应该可以工作:
s="http://user_name:user_password@example.com/gitproject.git"
sed 's~^\(.*//\)[^@]*@\(.*\)$~\1\2~' <<< "$s"
http://example.com/gitproject.git使用纯BASH的
echo "${s/*@/http://}"
http://example.com/gitproject.git发布于 2013-10-04 21:56:45
使用sed
$ sed "s#//.*@#//#g" <<< "http://user_name:user_password@example.com/gitproject.git"
http://example.com/gitproject.githttps://stackoverflow.com/questions/19182974
复制相似问题