我得到了不同域的 url 列表,我想用 sed、awk 或类似的东西删除主机名,只保留路径。其中没有带有端口或用户名@密码的网址。
输入:
http://www.example.com/
https://www.example.com/
http://example.com/blog/
https://example.com/blog/
https://www.example.co.uk/blog/
https://example.co.uk/blog/
https://sub.example.co.uk/blog/
https://www.example.com/blog/
https://www.example.com/cases/page/4/
https://www.example.com/cdn-cgi/challenge-platform/h/g/cv/result/7c9123dc38da6841
https://www.example.com/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
https://www.example.co.uk/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
https://sub.example.co.uk/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
输出应该是:
/
/
/blog/
/blog/
/blog/
/blog/
/blog/
/blog/
/cases/page/4/
/cdn-cgi/challenge-platform/h/g/cv/result/7c9123dc38da6841
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
我希望有人能帮助我,因为我只能找到正则表达式命令。我不知道如何将这些正确转换为 sed 或 awk 命令
与
perl
:将删除一个可选方案(同时处理
http://host/path
和//host/path
),然后//
是除此之外的所有字符(例如,/
将删除 thehost
和user:password@host:8080
in )。ftp://user:password@host:8080/pub
等价物
sed
可以是:在任何情况下,
s/pattern/replacement/
两者的运算符sed
都perl
采用正则表达式作为模式,所谓的基本正则表达式forsed
,perl 正则表达式forperl
(改进和扩展了扩展的正则表达式,如今许多sed
实现也支持选项)。-E
还有一个
URI
模块,perl
您可以使用该模块将 URI 解析为结构化对象。请注意,它会丢弃查询字符串(如中
http://host/path?query
)和片段(如中)(http://host/file.html#anchor
如果有)。如果要包含任何查询,请替换->path
为。->path_query
使用 linux coreutils 很容易做到这一点:
cut -d '/' -f 3- somefilewithyoururls.txt | sed 's/^/\//'
删除第三个之后的所有内容
/
,然后用 a 替换行的开头/
。不需要复杂的正则表达式。使用任何 sed: