【发布时间】:2020-08-04 16:58:42
【问题描述】:
我的目标是从 url 中提取主机和端口号。您能否更新我使用 perl 正则表达式或 sed/awk 等的任何示例?
echo "https://xxxx:port/services"
我正在寻找一个 oneliner 来提取主机和端口。
【问题讨论】:
-
到目前为止你尝试了什么?
我的目标是从 url 中提取主机和端口号。您能否更新我使用 perl 正则表达式或 sed/awk 等的任何示例?
echo "https://xxxx:port/services"
我正在寻找一个 oneliner 来提取主机和端口。
【问题讨论】:
perl -MURI -nle'$u = URI->new($_); printf "%s:%s\n", $u->host, $u->port'
http://stackoverflow.com/)【讨论】:
我经常这样做,所以我为此做了一个类似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
【讨论】: