Perl oneliner从URL提取主机和端口

Perl oneliner从URL提取主机和端口,perl,awk,Perl,Awk,我的目标是从url中提取主机和端口号。您能给我更新一些使用PerlRegex或sed/awk等的例子吗 echo "https://xxxx:port/services" 我正在寻找一个单线提取主机和端口 perl -MURI -nle'$u = URI->new($_); printf "%s:%s\n", $u->host, $u->port' 即使暗示了端口也有效。(例如http://stackoverflow.com/) 适用于HTTP、HTTPS、FTP和其他

我的目标是从url中提取主机和端口号。您能给我更新一些使用PerlRegex或sed/awk等的例子吗

echo "https://xxxx:port/services" 
我正在寻找一个单线提取主机和端口

perl -MURI -nle'$u = URI->new($_); printf "%s:%s\n", $u->host, $u->port'
  • 即使暗示了端口也有效。(例如
    http://stackoverflow.com/
  • 适用于HTTP、HTTPS、FTP和其他一些具有主机和端口概念的应用程序

我经常这样做,因此我为它制作了一个类似sprintf的实用程序:。如果您无法安装模块并轻松使用命令行程序,则此功能将不起作用,但在本地提取和重新排列内容将非常有用:

$ 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
我经常把它与我写给的一个小应用程序脚本一起使用:


到目前为止你都尝试了什么?太棒了,非常感谢你的快速回复。
$ safari-tabs 2>&1 | xargs url %h
www.google.com
github.com
dnschecker.org
securitytrails.com
st.aticpan.org