Replace identical lines with just one line at end in Notepad++

Solution 1:

You can use this regex in the search and replace dialogue:

^(.*?)$\s+?^(?=.*^\1$)

and replace with nothing.

You need to check the options "Regular expression" and ". matches newline":

Notepad++ Replace dialogue

Regex explanation:

^ matches the start of the row

(.*?) matches any characters 0 or more as few as possible. The matched row is stored, because of the brackets around and accessible using \1

$ matches the end of the row

\s+?^ this part matches all whitespace characters (newlines!) till the next Start of the row ==> This removes the newlines

(?=.*^\1$) this is a positive lookahead assertion. This is the important part in this regex, a row is only matched (and removed), when there is exactly the same row following somewhere else in the file.

Solution 2:

You could achieve this Sublime Text 2 (or any editor with full regex support - NOT Notepad++, sadly) with regex find & replace.

Find:

^(([^\n]+\n)*)([^\n]+)\n(([^\n]+\n)*)\3(\n.*)*$

Replace:

\1\4\3\6

Keep clicking "replace" until duplicate lines have been removed from the document. "Replace All" won't work here (or you'll have to press it multiple times).


Found a simpler solution that works for Notepad++ as well as other editors. Find with Regular expression mode and disable ". matches newline":

^(.*)$\s+(?=(^.*$\s+)*\1)

And replace with nothing. This is based off of @stema's answer below, but works even when the matched lines are substrings of following lines:

1st January 2013
//some text1
1st January 2013
//some text2
1st January 2013
//some text3
21st January 2013

Should properly return

//some text1
//some text2
1st January 2013
//some text3
21st January 2013