<?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[how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs]]></title><description><![CDATA[<p dir="auto">I have 122 files where i need to keep only the last 10 lines of code. i am really not looking forward to doing this manually. is there a way to automate this somehow??</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/21729/how-to-bulk-keep-only-last-10-lines-of-code-in-122-tabs</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 18:17:43 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/21729.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 24 Aug 2021 22:54:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Sat, 28 Aug 2021 19:25:41 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> ,</p>
<blockquote>
<p dir="auto"><code>^.*\Z</code></p>
</blockquote>
<p dir="auto">I knew that a <em>single</em> <code>^.*\Z</code> could match zero characters; I intended it that way in case the last line of the file was an empty line (ie, a file that has a final newline).  And that explains why one of the alternate versions that I played with but didn’t publish was off-by-one when I had that (though I didn’t put the two together at the time).</p>
<p dir="auto">But I was suprised that ten instances of <code>\Z</code> (from <code>(^.*\Z){10}</code>) could match the single end of the file.  I know it’s a “zero width” match, but I hadn’t thought about the fact that being “zero width” meant that two or more could match in a row.  But yes, I justed tested, and confirmed that you can have a regex with multiple EOF in a row and have it still match… or multiple <code>^</code> or <code>$</code> as well.  Interestingly. <code>\Z{10}</code> is invalid, but <code>(\Z){10}</code> is allowed – so you cannot multiple a true zero-width expression (which makes sense), but if you have a group that happens to contain only a zero-width expression, you can multiply that expression.</p>
<p dir="auto">Thanks for helping me learn something new today. :-)</p>
<blockquote>
<p dir="auto"><code>(?-s)^.*\R(?=(?:^.*\R|.+\Z){10})</code></p>
</blockquote>
<p dir="auto">I was trying to avoid repeating myself with having to put the dots in both alternations.  <code>(?-s)^.*\R(?=(?:^.*(?:\R|.\Z)){10})</code> works just as well… but yours actually requires less typing, so my adherence to <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" title="Don't Repeat Yourself" rel="nofollow ugc">DRY</a> made mine longer. :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/69357</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69357</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Sat, 28 Aug 2021 19:25:41 GMT</pubDate></item><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Sat, 28 Aug 2021 18:14:01 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/david-miles-viers" aria-label="Profile: david-miles-viers">@<bdi>david-miles-viers</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> and <strong>All</strong>,</p>
<p dir="auto"><strong>Peter</strong>, the problem with your regexes <strong><code>(?-s)^.*\R(?=(?:^.*(?:\R|\Z)){10})</code></strong> and <strong><code>(?-s)^.*\R(?=(?:^.*$\R?){10})</code></strong> is :</p>
<ul>
<li>
<p dir="auto">The part <strong><code>^.*\Z</code></strong> for the <strong>first</strong> one</p>
</li>
<li>
<p dir="auto">The part <strong><code>^.*$\R?</code></strong> for the <strong>second</strong> one</p>
</li>
</ul>
<p dir="auto">Indeed, these two sub-expressions can match a <strong>zero length</strong> string and as, there are both <strong>embedded</strong> in a <strong>positive look-ahead</strong>, they are equivalent to the regex <strong><code>(?x-s) (?= ( .{0} ) {10} )</code></strong> and, by extension, equivalent to the regex <strong><code>(?x-s) (?= ( .{0} ) )</code></strong>, which is a <strong>always-TRUE</strong> condition. For instance, the <strong><code>(?x-s) abc (?= ( .{0} ) ) def</code></strong> regex matches the <strong>abcdef</strong> string !</p>
<p dir="auto">Thus, your regexes <strong><code>(?-s)^.*\R(?=(?:^.*(?:\R|\Z)){10})</code></strong> and <strong><code>(?-s)^.*\R(?=(?:^.*$\R?){10})</code></strong> may, in some cases, just be identical to the <strong>shorter</strong> regex <strong><code>(?-s)^.*\R</code></strong> which, of course, selects any complete line, <strong>empty</strong> or <strong>not</strong></p>
<hr />
<p dir="auto">To avoid this <strong>side-effect</strong>, we can use the regex  <strong><code>(?-s)^.*\R(?=(?:^.*\R|.+\Z){10})</code></strong>, as <strong>each</strong> alternative ( <strong><code>.*\R</code></strong> and <strong><code>.+\Z</code></strong> ) of the look-ahead does <strong>not</strong> match an <strong>empty</strong> string</p>
<p dir="auto"><strong>Remark</strong> : It’s <strong>always</strong> preferable, when creating a regex expression, to <strong>not</strong> allow <strong>zero-lengh</strong> matchs ! For instance, the <strong><code>^[0-9]*\x20*</code></strong> would match any line <strong>beginning</strong> with <strong>digits</strong> and followed with <strong>space</strong> characters. But, the regexes <strong><code>^[0-9]+\x20*</code></strong> or even <strong><code>^[0-9]+\x20+</code></strong> are certainly <strong>better</strong> solutions to use !</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/69356</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69356</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Sat, 28 Aug 2021 18:14:01 GMT</pubDate></item><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Wed, 25 Aug 2021 13:23:35 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 in <a href="/post/69246">how to bulk "keep" only last 10 lines of code in 122 tabs</a>:</p>
<blockquote>
<p dir="auto">I tried it and found that it deleted all text, am I missing something?</p>
</blockquote>
<p dir="auto">Weird.  The test file that I tried last night worked perfectly with that.</p>
<p dir="auto">Some experimenting shows that either that regex or <code>(?-s)^.*\R(?=(?:^.*$\R?){10})</code> will work as I expect if there isn’t a final newline in the file; but if there is a final newline, then the lookahead will match fewer than 10 lines… and I cannot see why that is.</p>
<p dir="auto"><img src="https://i.imgur.com/QudqsVV.gif" alt="" class=" img-fluid img-markdown" /></p>
<blockquote>
<p dir="auto">Is there strong evidence that using capture groups vs. not using is an large contributing factor in regex overflow problems?</p>
</blockquote>
<p dir="auto">I thought there were times that it was solely the length of the capture group, but that was just my memory of things; I don’t have hard evidence.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/69254</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69254</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Wed, 25 Aug 2021 13:23:35 GMT</pubDate></item><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Wed, 25 Aug 2021 11:49:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> said in <a href="/post/69242">how to bulk "keep" only last 10 lines of code in 122 tabs</a>:</p>
<blockquote>
<p dir="auto">I had an alternate idea</p>
</blockquote>
<p dir="auto">I tried it and found that it deleted all text, am I missing something?</p>
<blockquote>
<p dir="auto">I think this will avoid regex space issues, because it doesn’t use capture groups</p>
</blockquote>
<p dir="auto">Is there strong evidence that using capture groups vs. not using is an large contributing factor in regex overflow problems?  Sure, it uses memory, but I was always under the impression that the bigger factor was the engine trying to keep track of what it has tried and what it has yet to try, to get a match.  Perhaps I haven’t thought about this enough.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/69246</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69246</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 25 Aug 2021 11:49:24 GMT</pubDate></item><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Wed, 25 Aug 2021 00:00:35 GMT]]></title><description><![CDATA[<p dir="auto">I had an alternate idea: delete a line if there are 10 or more lines after it.  I think this will avoid regex space issues, because it doesn’t use capture groups at all, and is only manipulating one row at a time:</p>
<ul>
<li>FIND = <code>(?-s)^.*\R(?=(?:^.*(?:\R|\Z)){10})</code></li>
<li>REPLACE = empty</li>
<li>SEARCH MODE = regular expression</li>
<li>REPLACE ALL IN OPEN FILES (or REPLACE IN FILES &gt; REPLACE ALL)</li>
</ul>
]]></description><link>https://community.notepad-plus-plus.org/post/69242</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69242</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Wed, 25 Aug 2021 00:00:35 GMT</pubDate></item><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Tue, 24 Aug 2021 23:41:44 GMT]]></title><description><![CDATA[<p dir="auto">Another thing to note is my suggestion may need a bit of tweaking if the last line of the file is left “hanging”, i.e. without a proper line-ending (e.g. CRLF) on it.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/69240</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69240</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 24 Aug 2021 23:41:44 GMT</pubDate></item><item><title><![CDATA[Reply to how to bulk &quot;keep&quot; only last 10 lines of code in 122 tabs on Tue, 24 Aug 2021 23:37:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/david-miles-viers" aria-label="Profile: David-Miles-Viers">@<bdi>David-Miles-Viers</bdi></a></p>
<p dir="auto">122 tabs…so you are talking about a “Replace All in All Opened Documents” operation.</p>
<p dir="auto">And with “keep only last 10 lines”, you have to be talking about using Regular expression search mode.</p>
<p dir="auto">One of the problems with regex is that if we are talking about a <em>large</em> amount of lines in any of the tabs, there is likely to be problems (with overflow of the regex engine).</p>
<p dir="auto">But you might try:</p>
<p dir="auto">Find: <code>(?s).*\R(?-s)((?:.*\R){10})\z</code><br />
Replace: <code>${1}</code></p>
<p dir="auto">which I found will transform this:</p>
<pre><code class="language-z">Must, life build dictionary art turn fit plane men magnet this which quick.
Wild heard hundred death same grow rule near, vowel; is.
Green dictionary up his baby eye soldier garden tell pretty; got see about block?
Pay after, reach mother pair spoke total her rock major rich always.
Swim, both drink; them, river, step discuss, last control probable have animal soft!
Interest summer each with raise letter wonder side top.
Enough bottom touch together circle to cost, glass shout this.
Pay ear soil draw chord plain beat planet lift; ground quick moon cow gray.
Require position story i chart teach story dad bread substance branch, home, yard.
Occur, space buy; multiply swim shell, paper catch say fine.
Tail, skin; rich little fact, compare stay sound food.
Person street duck million knew interest your condition spoke dark lake picture have.
Round; brown street happy magnet deep atom feel magnet sand!
Mark kill, plane receive whole determine; suffix; shoulder east nature.
Skill too buy, contain rest prove, sharp trouble complete, shoe basic occur.
Idea, proper; food row molecule early feet range slip toward door turn summer bird.
Strong colony place head doctor white, turn circle able swim quiet common clean.
Long see captain; small guess touch during held lift but!
Spread case way, sister fresh bring done energy heat multiply notice group parent.
Range poor certain trouble pose spread; suit energy bird circle sugar segment there.
Term star corn picture, nor three wide sing grow process hot.
Object idea was while rule supply, state born cow could create practice twenty yard cross.
Surprise still saw stick lie, wheel straight heart office captain row.
Atom, station deal touch until course matter try, all piece example look beat step little.
Common party bear now heart free drop enemy branch though?
</code></pre>
<p dir="auto">into:</p>
<pre><code class="language-z">Idea, proper; food row molecule early feet range slip toward door turn summer bird.
Strong colony place head doctor white, turn circle able swim quiet common clean.
Long see captain; small guess touch during held lift but!
Spread case way, sister fresh bring done energy heat multiply notice group parent.
Range poor certain trouble pose spread; suit energy bird circle sugar segment there.
Term star corn picture, nor three wide sing grow process hot.
Object idea was while rule supply, state born cow could create practice twenty yard cross.
Surprise still saw stick lie, wheel straight heart office captain row.
Atom, station deal touch until course matter try, all piece example look beat step little.
Common party bear now heart free drop enemy branch though?
</code></pre>
<p dir="auto">In other words, keep only the last 10 lines.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/69238</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/69238</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 24 Aug 2021 23:37:49 GMT</pubDate></item></channel></rss>