How to replace delete whole line containing specific characters?

Discussion Forums discussion How to replace delete whole line containing specific characters?

This topic contains 0 voices and has 4 replies.

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
Author Posts
May 29, 2008 at 7:56 pm #408

horus
Member

Suppose I want to delete all those whole lines containing the characters “abc”. By whole line, I mean even the newline character.

If I use the regex ^.*abc.*$ and replace it with nothing, empty lines will be left. But I don’t even want empty lines. What should I do?

May 30, 2008 at 12:23 pm #15907

horus
Member

I’ve found an alternative solution:

1. replacing those lines with blank lines

2. replacing double blank lines with single blank line

For 2., I’d like to replace multiple blank lines with one single blank line, but I don’t see how. I’ve tried (rn)+rn but this isn’t possible. Too bad.

May 30, 2008 at 2:00 pm #15908

simon
Key Master

That’s a pretty good workaround, this will all be solved once the multi-line regular expression support is done (which will be in for the next stable release).

Basically, do your regex replace to get the blank lines.

Switch to the backslash expressions mode and do the following Replace All until no matches are found:

Find Text: rnrn

Replace With: rn

It shouldn’t take too many presses of Replace All to get you there. You could even script this with PyPN.

June 21, 2008 at 4:26 pm #15909

kuukkeli
Member

I was trying to find out something to do when I saw this. Here’s a small PyPN script that removes all lines that include the text in current selection (I couldn’t figure out a way to read the contents of a search field):

@script("Delete lines with...", "Text")
def delete_lines():
"""Deletes all lines that contain current selection."""
sci = scintilla.Scintilla(pn.CurrentDoc())
search = sci.SelText
lng = len(search)

sci.BeginUndoAction()
while True:
sci.TargetStart = 0
sci.TargetEnd = sci.Length
pos = sci.SearchInTarget(lng, search)
if pos >= 0:
sci.GotoPos(pos)
sci.LineDelete()
else:
break
sci.EndUndoAction()

June 22, 2008 at 12:25 am #15910

kuukkeli
Member

Does the edit button disappear after a while? Anyways, I forgot to include imports on that previous code so I’ll just post it again in a “standalone” form.

import pn, scintilla
from pypn.decorators import script

@script("Delete lines with...", "Text")
def delete_lines():
"""Deletes all lines that contain current selection."""
sci = scintilla.Scintilla(pn.CurrentDoc())
search = sci.SelText
lng = len(search)

sci.BeginUndoAction()
while True:
sci.TargetStart = 0
sci.TargetEnd = sci.Length
pos = sci.SearchInTarget(lng, search)
if pos >= 0:
sci.GotoPos(pos)
sci.LineDelete()
else:
break
sci.EndUndoAction()

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.