【问题标题】:How to extract links from a text file? [duplicate]如何从文本文件中提取链接? [复制]
【发布时间】:2022-12-14 06:21:15
【问题描述】:

假设有一个文本文件test.txt。它包含文本和资源链接,例如https://example.com/kqodbjcuic49w95rofwjue。我怎样才能从那里只提取这些链接的列表? (最好通过 bash,但不是必需的)

我试过这个解决方案:

sed 's/^.*href="\([^"]*\).*$/\1/'

但这对我没有帮助。

【问题讨论】:

  • 你能展示来自 test.txt 的示例数据和你的预期输出吗

标签: bash grep


【解决方案1】:
grep -o "((?:(?:http|ftp|ws)s?|sftp)://?)?([^:/s.#?]+.[^:/s#?]+|localhost)(:d+)?((?:/w+)*/)?([w-.]+[^#?s]+)?([^#]+)?(#[w-]*)?" test.txt

将显示文件中的所有 URL。

(正则表达式来自 BSimjoo 的链接)

Grep 文本文件指南https://www.linode.com/docs/guides/how-to-grep-for-text-in-files/

【讨论】:

    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 2016-08-26
    • 2014-01-07
    • 2019-01-05
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多