gpt4 book ai didi

regex - 从文件中删除重复行

转载 作者:行者123 更新时间:2023-12-01 07:26:18 26 4
gpt4 key购买 nike

我有一个 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 作为我的编辑器。

最佳答案

使用 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

关于regex - 从文件中删除重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com