使用正则表达式删除重复行

我有一个html链接列表,其中大多数都是重复的,如下面这个例子 – >

> 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文本3.

谢谢

解决方法

使用awk非常简单:

awk '!seen[$0]++' file

这基本上意味着:

awk "!($0 in seen) {seen[$0];print}"

因此,如果该行不在数组中,它将添加并打印它.将跳过所有后续行(如果它们存在于数组中).

$cat file
> 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
$awk '!seen[$0]++' file
> http://example.com/some/a-test-link.html
> http://example.com/some/another-link.html
> http://example.com/some/again-link.html

相关文章

jquery.validate使用攻略(表单校验) 目录 jquery.validate...
/\s+/g和/\s/g的区别 正则表达式/\s+/g...
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母...
this.optional(element)的用法 this.optional(element)是jqu...
jQuery.validate 表单动态验证 实际上jQuery.validate提供了...
自定义验证之这能输入数字(包括小数 负数 ) <script ...