How do you get a macro recording to delete a certain number of lines?
-
Before I import data from a csv file into Excel, I first edit it with Notepad++. So far it’s working very well! Now I wanted to record a new macro that always deletes a certain number of lines in the middle of the text in every csv file. Unfortunately, the newly recorded macro works the same as the previous one. The new task that is supposed to delete a certain number of lines is ignored. Does anyone have a tip on how I can get this into the macro recording? If that’s not possible, I would ask you to program this function in the next possible update. Thanks in advance!
I’m using for this the newest version of Notepad++ v8.6 (64-bit) installed on usb stick as portable mode. -
@Tibor-Sütsch If you are using Notepad++'s find or search function to get to the spot in the middle of the CSV file then you can do a search/replace with the replacement part being nothing (empty) to delete those lines.
For example, let’s say you want to delete a line that contains the phrase “xyzzy” and the following two lines meaning you will be deleting three lines.
Search:
.*xyzzy.*\R.*\R.*\R
Replace:(nothing or empty)
The macro can do
Ctrl-H
.*xyzzy.*\R.*\R.*\Rtab
delete
Alt+A
Esc
If you regularly switch your search/find mode between regular expressions or and normal/extended then you should include
Alt-G
to activate regular expression mode. -
@mkupper
hello mkupper,
thank you for the fast reaction.
I already use search and replace in the macro I recorded so far. Which also works very well.
In addition, now I wanted to automatically delete everything from the 2nd to the 235th line. This leaves me with the remaining 67 lines of 301. But the problem is that there are no identical or identifiable groups of characters in these lines that need to be deleted. So it remains me only the orientation 2nd to the 235th lines. And as far as I understand it, unfortunately this no longer works with search and replace. -
@Tibor-Sütsch
Just thinking slightly out of the box here.I’m not on a PC currently to test, but if all the lines were numbered 1 onwards a regular expression would then bookmark the relevant numbered lines. Those would then be deleted and then the remaining lines would have those lines numbers removed.
I think that is all achievable using a macro.
I’ll test a bit later and hopefully come back with a solution.
Terry
-
You can delete lines
M
throughN
(inclusive) in a file by doing this:- calculate
X
=M
- 1 - calculate
Y
=N
-X
- ReplaceAll
\A(?:.*\R){X}\K(?:.*\R){Y}
with nothing (checkmarking Regular expression and Wrap around) – after plugging in the calculated numbers forX
andY
)
- calculate
-
@Alan-Kilborn
Hello Alan,I don’t understand the individual commands (not yet). ;) But I entered the values for my lines and it works exactly as I imagine! Perfect!
Thank you very much!
Wish you all a Merry Christmas! -
@Tibor-Sütsch, another way to do it that is similar to the first version I posted is:
Search:(.*xyzzy.*\R)(?:.*\R)*?(.*plugh.*)
Replace:nothing or empty
That will delete all lines starting with one that contains the string
xyzzy
and ending with one that containsplugh
. I put the(.*xyzzy.*\R)
and(.*plugh.*)
parts in parentheses so that you have the option of using either\1
and or\2
in the replacement part if you need to keep either of those. For example, to delete everything afterxyzzy
and up to and includingplugh
the replacement part would be\1
.