How to remove non-contiguous duplicate lines in vi without sorting?

I know how to remove adjacent duplicates in vi. Or

:%!uniq 

or

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

But I have a file that has data in random order, and there are several duplicate lines that are scattered throughout the file. How to remove all these duplicates without disturbing the row order? Should the first unique row be kept, and the next (or all the rest) duplicate should be deleted?

eg. cat file1

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

The conclusion should be:

Here looking at you, Kid.
Casablanca 
Go ahead, make my day. 
Dirty Harry
sleep 5
Yippee-ki-yay
+4
source share
1 answer

There is one awk liner for this:

$ awk '!a[$0]++' file
Here 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
+3

Source: https://habr.com/ru/post/1529992/


All Articles