如何在没有排序的情况下删除vi中不连续的重复行?

时间:2014-03-04 11:18:10

标签: shell unix vi

我知道如何在vi中删除连续的重复项。

:%!uniq 

:g/^\(.*\)$\n\1$/d). 

但我有一个文件,其中包含随机顺序的数据,并且有一些重复的行分散在整个文件中。如何在不影响线条顺序的情况下删除所有这些重复项?应该保留第一个唯一的行,并删除下一个(或全部)副本?

E.g。 cat file1

Here's looking at you, Kid.
Casablanca 
Here's looking at you, Kid.
Go ahead, make my day. 
Dirty Harry
sleep 5
Go ahead, make my day. 
Yippee-ki-yay

输出应为:

Here's looking at you, Kid.
Casablanca 
Go ahead, make my day. 
Dirty Harry
sleep 5
Yippee-ki-yay

1 个答案:

答案 0 :(得分:4)

有一个awk班轮非常少:

$ awk '!a[$0]++' file
Here's looking at you, Kid.
Casablanca 
Go ahead, make my day. 
Dirty Harry
sleep 5
Yippee-ki-yay

它跟踪数组a[]中处理的行。每当该行重新出现时,计数器已经为正,因此条件为假并且不打印该行。

如果您想在vim中运行它,请执行:

:%!awk '\!a[$0]++'
        ^^
       you have to escape the ! to be treated properly