【问题标题】:How to use grep for collecting URL links?如何使用 grep 收集 URL 链接?
【发布时间】:2014-04-02 08:21:47
【问题描述】:

假设我有一个名为 plaintext.txt 的文件,它有 2 行,内容为

you can be social online by logging in to http://www.facebook.com and http://plus.google.com
alternatively, you can also create an account in http://www.twitter.com

我知道我可以通过在命令行中发出 cat 语句来显示文件的全部内容,例如

cat plaintext.txt

我想从明文中收集所有的 URL 链接以便我可以显示

http://www.facebook.com
http://plus.google.com
http://www.twitter.com

我认为这个命令行语句类似于

cat plaintext.txt | grep something

但我不知道怎么做。

如何使用 grep 来收集 URL 链接?

【问题讨论】:

    标签: linux command-line grep cat


    【解决方案1】:
    sed 's/\s/\n/g' plaintext.txt | grep http:
    

    plaintext.txt 是包含您的两行的文件。

    【讨论】:

    • sed 's/\s/\n/g' 是什么意思?
    • 搜索空格并用换行符替换
    • \s 是空格,\n 是换行符。实际上,我们将所有单词分开,然后使用grep 查找http:
    【解决方案2】:

    您可以使用grep -o 选项。

    $ cat file
    you can be social online by logging in to http://www.facebook.com and http://plus.google.com
    alternatively, you can also create an account in http://www.twitter.com
    

    $ grep -o "http[^ ]*" file
    http://www.facebook.com
    http://plus.google.com
    http://www.twitter.com
    

    来自man page

    -o, --only-matching
                  Show only the part of a matching line that matches PATTERN.
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多