Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove duplicates AND the originals from a list in Sublime Text 3?

Edit > Permute Lines > Unique is great for removing duplicate from a list in Sublime Text. But what if I wanted to remove all matching results instead? For example:

james
james
bobby
mary
ann
ann

The above list of names would become:

bobby
mary

Because bobby and mary are the only names that only appear once.

like image 831
jkupczak Avatar asked Oct 27 '16 18:10

jkupczak


People also ask

How do you use a set to remove duplicate elements from a list?

Remove duplicates from list using Set. To remove the duplicates from a list, you can make use of the built-in function set(). The specialty of set() method is that it returns distinct elements.


1 Answers

If you don't mind your lines being sorted, you could do it like this:

  1. Edit > Sort Lines
  2. Find > Replace...
  3. Ensure RegEx mode is on
  4. Find What: (^.*$\n)\1+
  5. Replace With: (blank)

Although, sorting wouldn't be necessary if all the duplicates are next to each other, as per your example. e.g. it would even work with the following:

james
james
bobby
mary
ann
ann
james
james
james

Note that this regex requires the last line to have a trailing newline character, if it is a duplicate, otherwise it won't find it.

like image 51
Keith Hall Avatar answered Dec 02 '22 13:12

Keith Hall