Page 1 of 1
Delete lines that end with a specific string?
Posted: Sun Jan 22, 2023 12:09 pm
by terrypin
Can I use Regex to delete all lines in a text file that end with a specific string, in this case -a---(plus the EOL character)?
That's 'minus,a,minus,minus,minus,EOL'
Posted: Sun Jan 22, 2023 2:30 pm
by ben_josephs
Search | Replace... (
<F8>):
Find what: .*-a---\n
Replace with: [nothing]
[X] Regular expression
Replace All
Posted: Sun Jan 22, 2023 6:08 pm
by terrypin
Thanks a bunch, appreciate the speedy reply. I was plainly over-thinking it, as it looks so obvious now!
Posted: Sun Jan 22, 2023 10:22 pm
by ben_josephs
In fact, I often do this sort of thing in two steps:
1. Mark all the lines to be deleted:
Search | Find... (
<F5>):
Find what: -a---$
[X] Regular expression
Mark All
2. Delete the marked lines:
Edit | Delete | Bookmarked Lines
This gives me a better chance of avoiding doing something stupid (and the regex is slightly simpler).
Posted: Mon Jan 23, 2023 1:16 am
by AmigoJack
As per intention both approaches differ in that the former will not delete a last line in the file and the latter does.
Posted: Mon Jan 23, 2023 10:39 am
by ben_josephs
They only differ if the last line is not terminated with a newline (with TextPad 8.15.1 and 9.0.1).
Posted: Mon Jan 23, 2023 1:13 pm
by AmigoJack
Yes, that's the point. If the requirement includes CR or LF then your approach would still delete a potential last line of the file that wouldn't have a CR or LF, which would be wrong.
Posted: Mon Jan 23, 2023 1:16 pm
by ben_josephs
Pedantically, yes. But the omission of a newline at the end of a text file is usually an error.
Posted: Mon Jan 23, 2023 6:43 pm
by MudGuard
That could be fixed by adding a positive lookahead for the line break ...
Posted: Mon Jan 23, 2023 11:47 pm
by AmigoJack
ben_josephs wrote:the omission of a newline at the end of a text file is usually an error
Only in
POSIX and
Unix environments. However, TextPad
usually lives in... well... none of both.
No, I've learnt otherwise. Always implying a newline at the end is an important detail/requirement and is different from dealing with reality. Just because it
should be like that does by far not mean it is done like that by
everyone. And Windows is a whole different environment on top of that.
Re: Delete lines that end with a specific string?
Posted: Sat Feb 25, 2023 8:58 pm
by tbrock
Thank you gentlemen! You've added precious time back to my days!