加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:38:59 所属栏目:百科 来源:网络整理
导读:我有一个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://exampl
我有一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读