Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can I replace identical lines with just one line at the end in Notepad++? E.g. from:

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

to:

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

Also is it possible, if I have all dates from 1 to 30 or 31, to replace them individually at their last appearance?

share|improve this question

3 Answers 3

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.

share|improve this answer
    
Much simpler expression than what I had! The lookahead will erroneously match if the line it's looking for is an ending substring of a subsequent line though (since the . matches all will consume the beginning of a line) –  Darth Android Apr 30 '13 at 16:17
    
@DarthAndroid, thanks for the hint, fixed by adding anchors around the back reference. –  stema Apr 30 '13 at 18:52
    
@stema I tried your solution but it isn`t working. I noticed there is difference between screenshot expression and written one. ^(.*?)$\s+?^(?=.*^\1$) vs ^(.*?)$\s+^(?=.*^\1) I tried both of them also part by part it works till ^(.*?)$ i.e selection of whole line. But when I add \s next to it,npp selects rows containing and upto $ I have selected Regular expression. –  astroboy May 1 '13 at 17:08
    
also what changes should be made to match rows containing numbers in this case dates or year to avoid removing duplicate rows having characters. –  astroboy May 1 '13 at 17:19
    
@astroboy, have you also checked the second option ". matches newline"? For me both expressions work with your example text (The new one is only covering special case that a row ends with something that has been a row on its own) –  stema May 1 '13 at 20:12

Scanning for duplicate lines is, as far as I know, not possible in Notepad++ without any plugins, although you can make a manual effort a lot easier if the duplicates have a known format. For instance, dates formatted as '3rd February 2012' can be easily found (and replaced by empty strings) by matching against the regular expression ^\d+[snrt][tdh] [A-Z][a-z]* 20\d\d$.

As this answer does a great job explaining, the plugin TextFX is particularly useful for these procedures through its ability to temporarily sort lines.

share|improve this answer

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
share|improve this answer
    
Try a current Notepad++ version, since Version 6 it supports PCRE and no, this has nothing to do with sorcery. –  stema Apr 30 '13 at 6:09
    
@stema Using version 6.1.3 and the above expression does not work out of the box, though it works in my other editors. Found a simpler solution that works even for Notepad++, though. –  Darth Android Apr 30 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.