how to bulk "keep" only last 10 lines of code in 122 tabs
-
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??
-
122 tabs…so you are talking about a “Replace All in All Opened Documents” operation.
And with “keep only last 10 lines”, you have to be talking about using Regular expression search mode.
One of the problems with regex is that if we are talking about a large amount of lines in any of the tabs, there is likely to be problems (with overflow of the regex engine).
But you might try:
Find:
(?s).*\R(?-s)((?:.*\R){10})\z
Replace:${1}
which I found will transform this:
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?
into:
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?
In other words, keep only the last 10 lines.
-
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.
-
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:
- FIND =
(?-s)^.*\R(?=(?:^.*(?:\R|\Z)){10})
- REPLACE = empty
- SEARCH MODE = regular expression
- REPLACE ALL IN OPEN FILES (or REPLACE IN FILES > REPLACE ALL)
- FIND =
-
@PeterJones said in how to bulk "keep" only last 10 lines of code in 122 tabs:
I had an alternate idea
I tried it and found that it deleted all text, am I missing something?
I think this will avoid regex space issues, because it doesn’t use capture groups
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.
-
@Alan-Kilborn said in how to bulk "keep" only last 10 lines of code in 122 tabs:
I tried it and found that it deleted all text, am I missing something?
Weird. The test file that I tried last night worked perfectly with that.
Some experimenting shows that either that regex or
(?-s)^.*\R(?=(?:^.*$\R?){10})
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.Is there strong evidence that using capture groups vs. not using is an large contributing factor in regex overflow problems?
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.
-
Hello, @david-miles-viers, @peterjones, @alan-kilborn and All,
Peter, the problem with your regexes
(?-s)^.*\R(?=(?:^.*(?:\R|\Z)){10})
and(?-s)^.*\R(?=(?:^.*$\R?){10})
is :-
The part
^.*\Z
for the first one -
The part
^.*$\R?
for the second one
Indeed, these two sub-expressions can match a zero length string and as, there are both embedded in a positive look-ahead, they are equivalent to the regex
(?x-s) (?= ( .{0} ) {10} )
and, by extension, equivalent to the regex(?x-s) (?= ( .{0} ) )
, which is a always-TRUE condition. For instance, the(?x-s) abc (?= ( .{0} ) ) def
regex matches the abcdef string !Thus, your regexes
(?-s)^.*\R(?=(?:^.*(?:\R|\Z)){10})
and(?-s)^.*\R(?=(?:^.*$\R?){10})
may, in some cases, just be identical to the shorter regex(?-s)^.*\R
which, of course, selects any complete line, empty or not
To avoid this side-effect, we can use the regex
(?-s)^.*\R(?=(?:^.*\R|.+\Z){10})
, as each alternative (.*\R
and.+\Z
) of the look-ahead does not match an empty stringRemark : It’s always preferable, when creating a regex expression, to not allow zero-lengh matchs ! For instance, the
^[0-9]*\x20*
would match any line beginning with digits and followed with space characters. But, the regexes^[0-9]+\x20*
or even^[0-9]+\x20+
are certainly better solutions to use !Best Regards,
guy038
-
-
@guy038 ,
^.*\Z
I knew that a single
^.*\Z
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).But I was suprised that ten instances of
\Z
(from(^.*\Z){10}
) 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^
or$
as well. Interestingly.\Z{10}
is invalid, but(\Z){10}
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.Thanks for helping me learn something new today. :-)
(?-s)^.*\R(?=(?:^.*\R|.+\Z){10})
I was trying to avoid repeating myself with having to put the dots in both alternations.
(?-s)^.*\R(?=(?:^.*(?:\R|.\Z)){10})
works just as well… but yours actually requires less typing, so my adherence to DRY made mine longer. :-)