我想显示从https://www.gentoo.org/downloads/mirrors/
到终端的所有链接。
首先,脚本将网页wget
到一个名为index.html
的文件,然后grep
或sed
命令将简单地将所有https://
,http://
和ftp://
显示到终端。
有人能帮我一下这个命令吗?我知道这很简单,但是我对这两个命令都不是很熟悉。
我试过的:grep "<code>" index.html
输出:
<a href="ftp://mirrors.tera-byte.com/pub/gentoo"><code>ftp://mirrors.tera-byte.com/pub/gentoo</code></a>
<a href="http://gentoo.mirrors.tera-byte.com/"><code>http://gentoo.mirrors.tera-byte.com/</code></a>
<a href="rsync://mirrors.tera-byte.com/gentoo"><code>rsync://mirrors.tera-byte.com/gentoo</code></a>
如何删除链接后的空白空间,标签和所有不必要的文本?
如果您只想保留域名链接,您可以尝试grep
grep -Eo '[h|f]t*ps?://.[^<|>|"]*' index.html
这将只显示http
,https
和ftp
匹配
如果需要在<code>
块内匹配,这个sed
将工作
sed -En '/<code>/ {s|.*([h|f]t*ps?://.[^<|>|"]*).*|1|p}' index.html
您可以使用这种模式的grep:
grep -Po "(?<=<code>)(https?|ftp)(.*)(?=</code>)" index.html
前3行输出:
ftp://mirrors.tera-byte.com/pub/gentoo
http://gentoo.mirrors.tera-byte.com/
ftp://mirror.csclub.uwaterloo.ca/gentoo-distfiles/