【问题标题】:Perl oneliner to extract host and port from URL [closed]Perl 一种从 URL 中提取主机和端口的衬垫 [关闭]
【发布时间】:2020-08-04 16:58:42
【问题描述】:

我的目标是从 url 中提取主机和端口号。您能否更新我使用 perl 正则表达式或 sed/awk 等的任何示例?

echo "https://xxxx:port/services" 

我正在寻找一个 oneliner 来提取主机和端口。

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: perl awk


【解决方案1】:
perl -MURI -nle'$u = URI->new($_); printf "%s:%s\n", $u->host, $u->port'
  • 即使端口是隐含的也可以工作。 (例如http://stackoverflow.com/
  • 适用于 HTTP、HTTPS、FTP 和其他一些具有主机和端口概念的内容。

Specifying file to process to Perl one-liner

【讨论】:

  • 太棒了,非常感谢您的快速回复。
【解决方案2】:

我经常这样做,所以我为此做了一个类似sprintf 的实用程序:App::url。如果您无法安装模块并轻松使用命令行程序,这将无用,但在本地提取和重新排列内容非常有帮助:

$ url "%h:%p" http://www.example.com/a/b/c
www.example.com:80

$ url "%h:%p" http://www.example.com/a/b/c https://www2.example.org:444
www.example.com:80
www2.example.org:444

$ url "%h -> %I" http://www.example.com/a/b/c
www.example.com -> 93.184.216.34

我经常将它与我写给 get all the URLs from all the tabs in Safari 的一点 AppleScript 一起使用:

$ safari-tabs 2>&1 | xargs url %h
www.google.com
github.com
dnschecker.org
securitytrails.com
st.aticpan.org

【讨论】:

    猜你喜欢
    • 2022-10-04
    • 2012-09-11
    • 2013-03-27
    • 2017-07-05
    • 2012-03-21
    • 2014-01-27
    • 2014-08-13
    • 2015-03-08
    相关资源
    最近更新 更多