【发布时间】:2014-06-03 11:02:47
【问题描述】:
我有一个 URL 列表,其中大部分是重复的:
> http://example.com/some/a-test-link.html
> http://example.com/some/a-test-link.html
> http://example.com/some/another-link.html
> http://example.com/some/another-link.html
> http://example.com/some/again-link.html
> http://example.com/some/again-link.html
我不需要两次相同的链接,所以我需要删除重复的链接并只保留一个链接。如何使用正则表达式或sed 或awk 来做到这一点(我不确定哪种技术最好)。我使用 Ubuntu 作为操作系统,使用 Sublime Text 3 作为我的编辑器。
【问题讨论】:
-
使用 uniq 过滤重复项可能更容易:unixhelp.ed.ac.uk/CGI/man-cgi?uniq
-
你好@ChrisLaplante,是的,它可以使用
uniq -d filename.txt newname.txt。非常感谢!但是我们如何使用正则表达式来做到这一点?只是好奇。 -
unix 工具一次处理一行数据。
uniq -d是一种特殊情况,因为它保留前一行进行比较。sed可以使用正则表达式将一行与前一行进行比较,但如果您不是专家,这真的不是sed的设计目的。awk将保留上一行的副本,就像uniq -d一样,但您只能使用字符串比较运算符==,而不是正则表达式。如果您想在这方面做得更好,请阅读 O'Reillysed and awk的书。祝你好运。 -
@shellter,感谢您宝贵的cmets,我实际上是一个新手,边做边学!
-
不要拿那本书
sed and awk。您根本不需要一本书来了解 sed 的优点(单行上的简单替换),而 Robins 的书Effective Awk Programming, Third Edition更全面,更适合学习 awk。您还应该考虑约翰逊的书Shell Scripting Recipes。
标签: regex awk sed sublimetext3