如何使用grep收集URL链接?

时间:2014-03-01 04:52:33

标签: linux command-line grep cat

假设我有一个名为 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

我想收集明文中的所有网址链接,以便我可以显示

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

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

cat plaintext.txt | grep something

但我不知道如何。

如何使用grep收集网址链接?

2 个答案:

答案 0 :(得分:2)

sed 's/\s/\n/g' plaintext.txt | grep http:

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

答案 1 :(得分:0)

您可以使用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.