Delete lines that end with a specific string?

General questions about using TextPad

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
terrypin
Posts: 172
Joined: Wed Jul 11, 2007 7:50 am

Delete lines that end with a specific string?

Post 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'
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Search | Replace... (<F8>):
Find what: .*-a---\n
Replace with: [nothing]

[X] Regular expression

Replace All
terrypin
Posts: 172
Joined: Wed Jul 11, 2007 7:50 am

Post by terrypin »

Thanks a bunch, appreciate the speedy reply. I was plainly over-thinking it, as it looks so obvious now!
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post 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).
User avatar
AmigoJack
Posts: 482
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Post 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.
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post 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).
User avatar
AmigoJack
Posts: 482
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Post 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.
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Pedantically, yes. But the omission of a newline at the end of a text file is usually an error.
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

That could be fixed by adding a positive lookahead for the line break ...
User avatar
AmigoJack
Posts: 482
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Post 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.
tbrock
Posts: 1
Joined: Sat Feb 25, 2023 8:33 pm

Re: Delete lines that end with a specific string?

Post by tbrock »

Thank you gentlemen! You've added precious time back to my days!
Post Reply