【问题标题】:Extract all URLs that start with http or https and end with html from text file从文本文件中提取所有以 http 或 https 开头并以 html 结尾的 URL
【发布时间】:2015-12-13 12:16:16
【问题描述】:

我想使用 grep 命令从文本文件中提取每个以 http:// 开头(不确定我是否有 https://)并以 .html 结尾的链接。

我的问题是文件太大而且链接很多...

我试过了:

grep "/http:\/\/.*?\.html/"  filename.txt > newFile.txt

但我得到一个空文件,就像这样:

grep -Eo "(http|https)://[a-zA-Z0-9]./(html)" filename.txt > newFile.txt

谁能帮帮我?

为了确保我们在同一个轨道上,我想提取所有指向新文件的链接,并让它们每行 1 个。

谢谢。

最好的问候

【问题讨论】:

    标签: html regex http url grep


    【解决方案1】:

    你可以使用:

    grep -Eo "https?://\S+?\.html" filename.txt > newFile.txt
    

    这将匹配 https:// 之后和 .html 之前的 1 个或多个非空格字符

    【讨论】:

    • 哇,就是这个,你介意解释一下那个S+是什么吗?以及如何为唯一链接添加额外的参数?我的意思是在那个新文件中只有唯一的链接
    • 要获取唯一链接,请使用:grep -Eo "https?://\S+?\.html" filename.txt | sort -u > newFile.txt
    • 还有一件事:如果所有链接中包含“搜索”字样,我该如何排除它们?谢谢。
    • 您可以在grep 中使用-P(perl 正则表达式)选项,使用grep -Po 'https?://(?!\S*?search)\S+?\.html' filename.txt
    • 不,但谢谢。我有我想要的(大部分)......没有那么多带有“搜索”字样的链接......
    【解决方案2】:

    这对我有用:

    grep -oE '(http|https)://(.*).html' filename.txt > newFile.txt
    

    但是,如果我们在一行中有两个链接,我们会将这两个链接放在一行中

    http://site1.com/1.html</a>tralala<a href="http://site2.com/2.html
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多