How do I find and remove duplicate lines from a file using Regular Expressions?

Viewed 74624

This question is meant to be language agnostic. Using only Regular Expressions, can I find and replace duplicate lines in a file?

Please consider the follwing example input and the output that I want;

Input>>

11
22
22  <-duplicate
33
44
44  <-duplicate
55

Output>>

11
22
33
44
55
3 Answers

In RegexBuddy you can do this as follows:

  1. On the Library tab, load the RegexBuddy.rbl library if not loaded by default.
  2. In the lookup box, type "duplicate"
  3. Click the Use button to load the "delete duplicate lines" regex.
  4. On the GREP tab, specify the folder and file mask of the files you want to delete duplicates from.
  5. In the drop-down menu of the GREP button, select Execute.

If you're only doing this on one file, you can use the Test tab instead of the GREP tab. Load the file on the Test tab, and then click the Replace button in the main toolbar.

Related