提取 git URL 的域名使用 POSIX。

huangapple go评论97阅读模式
英文:

Extracting git url's domain with POSIX

问题

我试图构建从Git仓库中提取域的最可靠方法。
对于如下的URL:

  1. ssh://git@gitlab.com:22411/usage/project_100.git
  2. git://example.org/path/to/repo.git
  3. https://github.com/example/foobar.git
  4. http://github.com/example/foobar.git
  5. ssh://user@host.com/path/to/repo.git
  6. git://host.com/path/to/repo.git

我可以使用:

  1. echo $url | awk -F[/:] '{print $4}'

但对于像这样的仓库:

  1. "git@github.com:User/UserRepo.git"

它不起作用。但以下方法可以:

  1. echo $url | awk -v FS="(@|:)" '{print $2}'

是否有一种稳健的方法,我可以始终在POSIX中提取域?

英文:

I'm trying to build the most robust way to extract the domain from a git repo.
For urls like:

  1. ssh://git@gitlab.com:22411/usage/project_100.git
  2. git://example.org/path/to/repo.git
  3. https://github.com/example/foobar.git
  4. http://github.com/example/foobar.git
  5. ssh://user@host.com/path/to/repo.git
  6. git://host.com/path/to/repo.git

I can use:

  1. echo $url | awk -F[/:] '{print $4}'

But for repos like:

  1. "git@github.com:User/UserRepo.git"

It won't work. But the following does:

  1. echo $url | awk -v FS="(@|:)" '{print $2}'

Is there some robust way I could always exctract the domain in POSIX?

答案1

得分: 2

如果URL包含://,则知道要删除协议部分,然后删除从第一个/开始的所有内容。否则,如果它包含@,则假设它是您的第二种情况,并删除包括@在内的所有内容,然后删除从:开始的所有内容。其他情况可以根据需要添加。

  1. url="..."
  2. case $url in
  3. *://*)
  4. domain=${url#*://}
  5. domain=${domain%%/*}
  6. ;;
  7. *@*:*)
  8. domain=${url#*@}
  9. domain=${domain%%:*}
  10. ;;
  11. esac
英文:

If the URL contains ://, you know to drop the protocol, then drop everything from the first / onwards. Otherwise, if it contains @, assume it is your second case, and drop everything up to and including the @, then everything from the : onwards. Other cases can be added as necessary.

  1. url="..."
  2. case $url in
  3. *://*)
  4. domain=${url#*://}
  5. domain=${domain#*@}
  6. domain=${domain%%/*}
  7. ;;
  8. *@*:*)
  9. domain=${url#*@}
  10. domain=${domain%%:*}
  11. ;;
  12. esac

答案2

得分: 2

使用sed。我从s///切换到s|||

  1. sed 's|.*//||; s|.*@||; s|/.*||; s|:.*||' file

输出:

  1. <pre>
  2. gitlab.com
  3. example.org
  4. github.com
  5. github.com
  6. host.com
  7. host.com
  8. </pre>
英文:

With sed. I switched from s/// to s|||.

  1. sed &#39;s|.*//||; s|.*@||; s|/.*||; s|:.*||&#39; file

output:
<pre>
gitlab.com
example.org
github.com
github.com
host.com
host.com
</pre>

答案3

得分: 1

perl -pe &#39;s{.*//([^/]+@)?([^:/]+).*}{$2}&#39; input-file

英文:

Perl version :

  1. perl -pe &#39;s{.*//([^/]+@)?([^:/]+).*}{$2}&#39; input-file

答案4

得分: 0

你可以很容易地使用sed来做到这一点。

echo $url | sed -E '&#39;s/.*\:\/\/(.*)@?.*\:.*/\1 /&#39;' | awk -F@ '&#39;{print $1}&#39;'

英文:

You can do that with sed easily.

echo $url | sed -E &#39;s/.*\:\/\/(.*)@?.*\:.*/\1 /&#39; | awk -F@ &#39;{print $1}&#39;

huangapple
  • 本文由 发表于 2020年1月6日 20:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定