用 IP 地址替换部分 URL

spa*_*spa 1 linux sed

我想做的是下面的事情。我有一个网址,比如http://www.google.com/one/two/three

我需要提取主域名“www.google.com”,将其提供给 nslookup(因为 nslookup/dig 似乎不适用于完整的 URL),然后将 URL 替换为已解析的 IP 地址。eg

$ echo "http://www.google.com/one/two/three" | sed "s/<pattern>//g" 
$ www.google.com
Run Code Online (Sandbox Code Playgroud)

问题是“http://”可能并不总是存在。进而

$ echo "http://www.google.com/one/two/three" | sed "s/<pattern>//g" 
$ http://11.22.33.44/one/two/three
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供任何相关链接或相关示例吗?

anu*_*ava 5

试试这个 sed 命令:

echo "http://www.google.com/one/two/three" | sed -r 's#(https?://)?([^/]+).*#\2#'
Run Code Online (Sandbox Code Playgroud)

输出:

www.google.com
Run Code Online (Sandbox Code Playgroud)

当您获取 IP 地址时:

$> IP="11.22.33.44"
$> echo "https://www.google.com/one/two/three" | sed -r "s#(https?://)?([^/]+)(.*)#\1$IP\3#"
https://11.22.33.44/one/two/three
Run Code Online (Sandbox Code Playgroud)

这将工作http://,https://或没有任何https?://的URL之前。