<?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[Replacing strings based on line numbers]]></title><description><![CDATA[<p dir="auto">Dear Notepad++ users!</p>
<p dir="auto">I have a simple task and a simple question, but I can’t get a simple answer) (maybe I’m not formulating the request so precisely in either Russian or English). The point is this. How can I automate such a routine task as copying one line in a text file and replacing another line with it. I usually do this between two txt files, but it can be done in one. So, for example, I need to replace 1 line with the 4th, 2nd with the 5th, 3rd with the 6th, etc. Regardless of the content of the lines. In other words, is it possible to somehow do automatic line-by-line replacement based on line numbers?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/25719/replacing-strings-based-on-line-numbers</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 20:18:01 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/25719.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 26 Apr 2024 05:16:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Replacing strings based on line numbers on Fri, 26 Apr 2024 08:16:37 GMT]]></title><description><![CDATA[<p dir="auto">Hello, @evguenii-baranov and <strong>All</strong>,</p>
<p dir="auto">Quite <strong>easy</strong> with <strong>regular expression</strong> !</p>
<hr />
<p dir="auto">So, let’s suppose that your file contains <strong>only</strong> the simple text, below :</p>
<pre><code class="language-diff">This is
a very
small bunch
of text
to see
if it
works nicely
</code></pre>
<ul>
<li>
<p dir="auto">Open your file in Notepad++</p>
</li>
<li>
<p dir="auto">Place the <strong>caret</strong> ( cursor ) on the <strong>first</strong> line which must be <strong>replaced</strong> ( Line <strong><code>This is</code></strong> ). This point is <em>IMPORTANT</em> !</p>
</li>
<li>
<p dir="auto">Open the <strong>Replace</strong> dialog ( <strong><code>Ctrl + H</code></strong> )</p>
</li>
<li>
<p dir="auto"><strong>Un-check</strong> all the <strong>box</strong> options ( <em>IMPORTANT</em> )</p>
</li>
<li>
<p dir="auto">SEARCH <strong><code>(?-is).+\R(?=(?:.+\R){2}(.+\R))</code></strong></p>
</li>
<li>
<p dir="auto">REPLACE <strong><code>$1</code></strong></p>
</li>
<li>
<p dir="auto">Select  the <strong><code>Regular expression</code></strong> search <strong>mode</strong></p>
</li>
<li>
<p dir="auto">Click <em>ONCE</em> <strong>only</strong> on the <strong><code>Replace All</code></strong> button</p>
</li>
</ul>
<p dir="auto">=&gt; You should get your <strong>expected</strong> text :</p>
<pre><code class="language-diff">of text
to see
if it
works nicely
to see
if it
works nicely
</code></pre>
<p dir="auto">Then, you just have to <strong>get rid</strong> of the <strong>three last</strong> lines which are <strong>repeated</strong> :</p>
<pre><code class="language-diff">of text
to see
if it
works nicely
</code></pre>
<hr />
<p dir="auto">In a general way, if you must replace the <strong>first</strong> line by the line <strong><code>N</code></strong>, use the following <strong>generic</strong> regex :</p>
<ul>
<li>
<p dir="auto">SEARCH <strong><code>(?-is).+\R(?=(?:.+\R){</code>N-2<code>}(.+\R))</code></strong></p>
</li>
<li>
<p dir="auto">REPLACE <strong><code>$1</code></strong></p>
</li>
</ul>
<p dir="auto">And replace the <strong><code>N-2</code></strong> expression with its <strong>appropriate</strong> number !</p>
<p dir="auto">so, for example, the different <strong>regex</strong> searches,  below, may be used :</p>
<ul>
<li>
<p dir="auto"><strong><code>(?-is).+\R(?=(?:.+\R){0}(.+\R))</code></strong></p>
</li>
<li>
<p dir="auto"><strong><code>(?-is).+\R(?=(?:.+\R){1}(.+\R))</code></strong></p>
</li>
<li>
<p dir="auto"><strong><code>(?-is).+\R(?=(?:.+\R){5}(.+\R))</code></strong></p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/94497</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/94497</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 26 Apr 2024 08:16:37 GMT</pubDate></item><item><title><![CDATA[Reply to Replacing strings based on line numbers on Fri, 26 Apr 2024 08:09:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/30680">@Evgenii-Baranov</a><br />
Not entirely clear to me. From your description, it seems like all lines except the last three are shifted upwards to replace the earlier lines. However, could you possibly give a few more examples to illustrate this better? This is how I understand the result based on the information provided:</p>
<pre><code>Line 1: This is line 4 before replacing.
Line 2: This is line 5 before replacing.
Line 3: This is line 6 before replacing.
Line 4: This is line 7 before replacing.
Line 5: This is line 8 before replacing.
Line 6: This is line 9 before replacing.
Line 7: This is line 10 before replacing.
Line 8: This is line 11 before replacing.
Line 9: This is line 12 before replacing.
Line 10: This is line 10 before replacing.
Line 11: This is line 11 before replacing.
Line 12: This is line 12 before replacing.
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/94496</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/94496</guid><dc:creator><![CDATA[Thomas Knoefel]]></dc:creator><pubDate>Fri, 26 Apr 2024 08:09:04 GMT</pubDate></item></channel></rss>