<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Remove duplicate lines from unsorted, keeping first]]></title><description><![CDATA[<p dir="auto">I’ve seen some techniques here for using regular expressions to remove duplicate lines from an unsorted file, but these all seem to show how to keep the LAST occurrence of the duplicated line. I need to do this but keep the FIRST occurrence.  Does anyone know how that can be done?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/14835/remove-duplicate-lines-from-unsorted-keeping-first</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 01:52:30 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/14835.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Nov 2017 14:32:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Sun, 21 Jan 2018 01:32:58 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/sepehr-e" aria-label="Profile: sepehr-e">@<bdi>sepehr-e</bdi></a>,</p>
<p dir="auto">I must admit that sometimes regexes, involving <strong>great</strong> amount of text, may, wrongly, get an <strong>unique</strong> match, which represents <strong>all</strong> the file contents :-(( This case may also happen, in case of regexes with <strong>recursive</strong> patterns, inside !</p>
<p dir="auto">I can’t clearly explain this behaviour. May be, it’s related to a <strong>matched</strong> range of characters, that <strong>exceeds</strong> a limit. It could also depends on the <strong>RAM</strong> amount or because of specific N+++ features, like the <strong>periodic backup</strong> !</p>
<hr />
<p dir="auto">Practically, you could use the regex, below, which implies an other <strong>condition</strong> : the <strong><code>\1+</code></strong> block of lines, which is to be <strong>deleted</strong>, must, follow, most of the time, some <strong>End of line</strong> characters !</p>
<p dir="auto"><strong><code>(?-s)^(.+\R)(?s).*?\R?\K\1+</code></strong></p>
<p dir="auto">Despite you didn’t say anything about your <strong>working</strong> file, but it could help ?!</p>
<p dir="auto">Note that the syntax <strong><code>\R</code></strong> must be <strong>optional</strong> ( =&gt; the form <strong><code>\R?</code></strong> ) in case of a block of consecutives <strong>identical</strong> lines, as for instance :</p>
<pre><code class="language-diff">456
789
123
123
123
123
000
</code></pre>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/29630</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/29630</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Sun, 21 Jan 2018 01:32:58 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Fri, 19 Jan 2018 15:23:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a>,<br />
Why does sometimes the <code>(?-s)^(.+\R)(?s).*?\K\1+</code> makes Notepad++ think the whole text is duplicated and needs to be deleted?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/29587</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/29587</guid><dc:creator><![CDATA[Sepehr E]]></dc:creator><pubDate>Fri, 19 Jan 2018 15:23:21 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Thu, 21 Dec 2017 21:08:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> said:</p>
<blockquote>
<p dir="auto">Is it important that the last line have a line-ending on it if it is otherwise a duplicate…?</p>
</blockquote>
<p dir="auto">Mostly <em>No</em>…but <em>maybe</em> ?</p>
<p dir="auto">Say we start with this data:</p>
<pre><code class="language-lua">value1[CR][LF]
value2[CR][LF]
value2[CR][LF]
value4[CR][LF]
value3[CR][LF]
value3[CR][LF]
value2[CR][LF]
value4
</code></pre>
<p dir="auto">Notice that the last <code>value4</code> does NOT have a line-ending on it.</p>
<p dir="auto">Then, using the technique above, and after multiple <strong>Replace All</strong> actions, we are left with this:</p>
<pre><code class="language-lua">value1[CR][LF]
value2[CR][LF]
value4[CR][LF]
value3
</code></pre>
<p dir="auto">Thus, the final <code>value4</code> was detected as a duplicate and removed, even though the line itself wasn’t an <em>exact</em> duplicate in the original data.</p>
<p dir="auto">Note, however, that the new last line (containing <code>value3</code>) now does not have a line-ending…when all the <code>value3</code> lines in the original file did.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/28867</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28867</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Thu, 21 Dec 2017 21:08:23 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Mon, 27 Nov 2017 16:16:16 GMT]]></title><description><![CDATA[<p dir="auto">English<br />
<a href="http://rgho.st/8C8jFTw4b" rel="nofollow ugc">http://rgho.st/8C8jFTw4b</a></p>
]]></description><link>https://community.notepad-plus-plus.org/post/28308</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28308</guid><dc:creator><![CDATA[AZJIO AZJIO]]></dc:creator><pubDate>Mon, 27 Nov 2017 16:16:16 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Mon, 27 Nov 2017 14:19:04 GMT]]></title><description><![CDATA[<p dir="auto"><a href="http://rgho.st/6GD58rS8H" rel="nofollow ugc">http://rgho.st/6GD58rS8H</a><br />
See the sections AutoIt and Scripting.Dictionary.<br />
If satisfied, I will do it in English</p>
]]></description><link>https://community.notepad-plus-plus.org/post/28305</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28305</guid><dc:creator><![CDATA[AZJIO AZJIO]]></dc:creator><pubDate>Mon, 27 Nov 2017 14:19:04 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Sun, 26 Nov 2017 21:29:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> said:</p>
<blockquote>
<p dir="auto">(?-s)^(.+\R)(?s).*?\K\1+</p>
</blockquote>
<p dir="auto">One of the things that I’m faced with is the need to move regular expressions between Notepad++ and Python code.  As Python doesn’t support <code>\K</code> I lean toward the regexes for this that don’t contain it.  Note that Python doesn’t support the <code>\R</code> syntax either, but I used it earlier.  But as <code>\R</code> is just a simple abbreviation and <code>\K</code> can have bigger logic implications, I’m allowed a little license with the <code>\R</code>.  :-D Of course, the longer “Cookbook” regexes have wider applicability than even N++ and Python.</p>
<blockquote>
<p dir="auto">exact negative class [^\r\n\f\x85\x{2028}\x{2029}]</p>
</blockquote>
<p dir="auto">Well, I don’t care about any of those beyond the <code>\n</code>…but maybe somebody does!  :-D</p>
<blockquote>
<p dir="auto">…ONLY, inside the part, within parentheses</p>
</blockquote>
<p dir="auto">Now THAT I didn’t know.  Nice.  But I still tend to like <code>[\s\S]</code> and <code>[^\r\n]</code> (and their close relatives that you pointed out).</p>
]]></description><link>https://community.notepad-plus-plus.org/post/28276</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28276</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Sun, 26 Nov 2017 21:29:15 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Sun, 26 Nov 2017 20:04:37 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/scott-sumner" aria-label="Profile: scott-sumner">@<bdi>scott-sumner</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">An <strong>other</strong> formulation of this regex, could be :</p>
<p dir="auto">SEARCH <strong><code>(?-s)^(.+\R)(?s).*?\K\1+</code></strong></p>
<p dir="auto">REPLACE <strong><code>EMPTY</code></strong></p>
<p dir="auto">OPTIONS <strong><code>Wrap around</code></strong> and <strong><code>Regular expression</code></strong> <strong>set</strong></p>
<p dir="auto">ACTION : Click, <strong>repeatedly</strong>, on the <strong><code>ALT + A</code></strong> shortcut ( <strong>Replace All</strong> )</p>
<p dir="auto">( <strong>Easy</strong> to memorize : <strong><code>\K</code></strong> as <code>K</code>ilborn ! )</p>
<hr />
<p dir="auto">So, for instance, from the <strong>initial</strong> text, below, with a <strong>line break</strong> after the <strong>last</strong> item <strong>123</strong>  :</p>
<pre><code class="language-diff">123
456
123
789
789
000
789
456
abc
123
123
456
456
456
789
999
123

</code></pre>
<p dir="auto">We get :</p>
<pre><code class="language-diff">123
456
789
000
abc
999
</code></pre>
<hr />
<p dir="auto"><strong>Scott</strong>, the regex, which represents a <strong>single standard</strong> character, is <strong><code>(?-s).</code></strong>. It can, also, be replaced by the <strong>exact negative</strong> class <strong><code>[^\r\n\f\x85\x{2028}\x{2029}]</code></strong>. So, the regex <strong><code>[^\r\n]</code></strong> is just an <strong>easy</strong> approximation :-)</p>
<p dir="auto">And the regex <strong><code>(?s).</code></strong>, which represents <strong>any</strong> character, at all, is <strong><code>(?s).</code></strong>. It can, also, be replaced by <strong>any</strong> of the regexes <strong><code>[\s\S]</code></strong> , <strong><code>[\d\D]</code></strong> , <strong><code>[\l\L]</code></strong> , <strong><code>[\u\U]</code></strong> , <strong><code>[\w\W]</code></strong> , <strong><code>[\h\H]</code></strong> or <strong><code>[\v\V]</code></strong></p>
<hr />
<p dir="auto"><strong>Scott</strong>, you said, too :</p>
<blockquote>
<p dir="auto">I like these as they put all the functionality in one place in the regex</p>
</blockquote>
<p dir="auto">But, if you placed the <strong><code>(?s)</code></strong> syntax, inside <strong>round</strong> parentheses, along with a <strong>regex</strong> expression, <strong><code>(?s)</code></strong> acts, ONLY, inside the part, within <strong>parentheses</strong> :-))</p>
<p dir="auto">For instance, let’s consider the text :</p>
<pre><code class="language-diff">blablah
A simple
abc
123
456
789
xyz
Test
blablah
</code></pre>
<p dir="auto">And imagine the regex <strong><code>(?-s).+\R((?s)abc.+xyz\R).+</code></strong></p>
<ul>
<li>
<p dir="auto">In the <strong>first</strong> and, above all, the <strong>last</strong> part of the regex, the <strong>dot</strong> <strong><code>.</code></strong> means a <strong>standard</strong> character</p>
</li>
<li>
<p dir="auto">In the <strong>middle</strong> part, surrounded by <strong>parentheses</strong>, the <strong>dot</strong> means <strong>any</strong> character !</p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/28274</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28274</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Sun, 26 Nov 2017 20:04:37 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Sun, 26 Nov 2017 19:08:07 GMT]]></title><description><![CDATA[<p dir="auto">Excellent.  :)</p>
<p dir="auto">Is it important that the last line have a line-ending on it if it is otherwise a duplicate of exactly one line that comes before it?  I guess I will find out.</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/28273</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28273</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sun, 26 Nov 2017 19:08:07 GMT</pubDate></item><item><title><![CDATA[Reply to Remove duplicate lines from unsorted, keeping first on Sun, 26 Nov 2017 15:03:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a></p>
<p dir="auto">So the <em><strong>Regex Cookbook</strong></em> (buy it!) gives a couple of regular-expression replacement scenarios for this:</p>
<p dir="auto"><strong>Find-what</strong> zone: <code>(?s)^([^\r\n]*)$(.*?)(?:(?:\r?\n|\r)\1$)+</code><br />
<em><strong>OR</strong></em><br />
<strong>Find-what</strong> zone: <code>(?-s)^(.*)$([\s\S]*?)(?:(?:\r?\n|\r)\1$)+</code><br />
For either choice the <strong>Replace-with</strong> zone is set to <code>\1\2</code></p>
<p dir="auto">Some changes may be made to these expressions for more typical use in Notepad++:</p>
<p dir="auto"><strong>Find-what</strong> zone: <code>(?-s)^(.*)$(?s)(.*?)(?:\R\1$)+</code><br />
<strong>Replace-with</strong> zone: <code>\1\2</code><br />
<strong>Wrap around</strong> checkbox:  ticked<br />
<strong>Action</strong>:  Press <strong>Replace All</strong> button REPEATEDLY until status bar indicates: “<em>Replace All: 0 occurrences were replaced.</em>”</p>
<p dir="auto">But…there are some interesting things to note in the Cookbook regexes:</p>
<ul>
<li><code>[^\r\n]</code> may be used in place of “<code>(?-s)</code> with a later occurring <code>.</code>” to mean “any character but not including (or across) line-ending characters”</li>
<li><code>[\s\S]</code> may be used in place of “<code>(?s)</code> with a later occurring <code>.</code>” to mean “any character at all (including line-ending characters)”</li>
</ul>
<p dir="auto">I like these as they put all the functionality in one place in the regex.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/28265</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/28265</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Sun, 26 Nov 2017 15:03:37 GMT</pubDate></item></channel></rss>