Finding and deleting a series of lines
-
Hello @james and All,
To search and delete some
non-empty
lines before and after a specific line, containing the expression to search for, use the generic S/R, below :SEARCH
(?-is)(^.+\R){
n}.*
Expression.*\R(?1){
m}
REPLACE
Leave EMPTY
where
n
represents the number of lines before the line containing Expression (n >= 0
)and
m
represents the number of lines after the line containing Expression (m >= 0
)For instance, from your example :
-
The regex
(?-is)^(.+\R){3}.*1234.*\R(?1){0}
would search for the3
lines before a line containing the number1234
and0
line after and replace all this block of lines with the line containing the string1234
only -
The regex
(?-is)^(.+\R){1}.*1234.*\R(?1){4}
would search for one line before any line containing the number1234
and four lines after and replace all this block of lines with the line1234
only
Best regards,
guy038
-
-
How am I supposed to input those commands in notepad++? There’s no cli interface that I know of, and there doesn’t seem to be a place in the gui version. Unless there’s a guide you could share, that would be great. Thanks
-
It would be a manual entry type of thing, with the affected places shown in yellow:
For automation, you’d have to turn to a scripting plugin, I suppose, if Replace in Files (see the Find in Files tab of the above shown window) doesn’t meet your need.
-
Now it’s saying there’s no occurances of that command ((?-is)(^.+\R){n}.Expression.\R(?1){m}). I assume it’s because it’s searching for it as a string, not as a command. How would I input the command from the earlier message to do this? Thank you for your help
-
it’s saying there’s no occurances of that command ((?-is)(^.+\R){n}.Expression.\R(?1){m}).
Well, hopefully you understand that this is a formula and not something you would directly input:
This, however, is an application of that formula, and thus could be directly entered into the Find what box:
(?-is)^(.+\R){1}.*1234.*\R(?1){4}
-
@James said in Finding and deleting a series of lines:
I assume it’s because it’s searching for it as a string, not as a command.
I’m confused by this part.
There is no searching by command, there is only searching by string. -
I should’ve phrased that better. When I try inputting that command in the find what section, it seems to be searching for it as a string, rather than as some sort of formula like you said.
-
@James said in Finding and deleting a series of lines:
it seems to be searching for it as a string, rather than as some sort of formula like you said.
This is a regular expression so the search mode MUST be regular expression. That was the 3rd highlight in the image @Alan-Kilborn provided above.
Terry
-
@Alan-Kilborn said in Finding and deleting a series of lines:
There is no searching by command, there is only searching by string.
I suppose that statement I made was misleading, as a “regular expression” rather is more like a formula/command than it is like a string. Sorry for any confusion I brought. :-)
-
Well even when I use the find what section and regular expression options, it seems to be searching for it as a string regardless.
-
Hmm. Yea, I see what you mean. I was using the formula to try to find the text “copy” in the N++ license.txt file. Even though I tweaked the formula a bit so that it would ignore case, I was getting no hits.
As @guy038 wrote the formula, I’ll let him comment on what might not quite be right there.
-
@James ,
It works on a single file for me.
eight seven six five four three two one 1234 one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen
FIND =
(?-is)^(.+\R){7}.*1234.*\R(?1){14}
Taking @Alan-Kilborn’s example, I see the problem. The
(.+\R)
assumes there’s at least one character on every line before or after the matching text. The license.txt has blank lines throughout, so there usually aren’t many lines before and after. So change to(.*\R)
=> FIND =(?i-s)^(.*\R){7}.*copy.*\R(?1){14}
(I also changed to(?i-s)
so that thecopy
was case-insensitive, as Alan mentioned.)So if your real data has any lines that are blank (just newlines), you will need to use
.*
instead of.+
-
Hello @james, @lan-kilborn, @terry-r, @peterjones and All,
@james : Thanks to @peterjones, you have the right solution !
So, in summary :
-
To delete some
non-empty
lines before and/or after a line, containing Expression, with this exact case :- SEARCH
(?-is)(^.+\R){
n}.*
Expression.*\R(?1){
m}
- SEARCH
-
To delete some
non-empty
lines before and/or after a line, containing Expression, whatever its case- SEARCH
(?i-s)(^.+\R){
n}.*
Expression.*\R(?1){
m}
- SEARCH
-
To delete some lines, possibly
empty
, before and/or after a line, containing Expression, with this exact case :- SEARCH
(?-is)(^.*\R){
n}.*
Expression.*\R(?1){
m}
- SEARCH
-
To delete some lines, possibly
empty
, before and/or after a line, containing Expression, whatever its case- SEARCH
(?i-s)(^.*\R){
n}.*
Expression.*\R(?1){
m}
- SEARCH
-
where
n
represents the number of lines before the line containing Expression (n >= 0
) -
and
m
represents the number of lines after the line containing Expression (m >= 0
)
Best Regards,
guy038
-
-
Actually, what you’ve specified is for finding some… , not deleting some…
To delete, we need to specify:
- SEARCH (as above)
- REPLACE leave this field blank
:-)