Batch deleting or replacing based on line numbers in multiple files
-
Cutting right to an example:-
Line No. 1 (start of text files) Line No. 2 Line No. 3 Line No. 4 ... Line No. 68 Line No. 69 ... Line No. 77 .... Line No. 80 (end of text files)
These lines numbers are shown on the right hand side of Notepad++. Contents of every lines may not be exactly the same across multiple files. The end of text files doesn’t necessarily ends at line no. 80. It could be more or less across these multiple files.
Case 1: In this occasion I want to delete lines no. 68 to 77 in multiple files, regardless of their contents, empty or not (as these contents may vary accross multiple files).
Case 2: I want to replace say, line no. 3 to 5 (again, the contents in these lines may not be the same) with 3 new lines of fixed contents like, for example:
New Line 3: awefew
New Line 4: gfwaef
New Line 5: wefeAre they possible to achieve in Notepad++?
If not, I am open to other suggestions. -
@golden000 said in Batch deleting or replacing based on line numbers in multiple files:
right hand side of Notepad++
My bad, it’s left hand side of notepad++
I dont know why I typed it wrong -
The transformations are:
(skip 3 lines and) delete line 4 F: \A(^.*\R){3}\K^.*\R R: (empty) (skip 1st 67 lines and) delete lines 68-77 F: \A(^.*\R){67}\K(^.*\R){10} R: (empty) (skip 4 lines and) replace line 5 F: \A(^.*\R){4}\K^.* R: This is the replacement for the removed line
Modify the {numbers} to fit your needs.
To see how they work on the active tab, in each case, bring up the Replace dialog (Ctl-h) and set Search Mode to Reg expr, leaving the box to its right unchecked. Fill in the Find & Replace texts. With (empty) ensure there are no invisible spaces. In each case even though you maybe doing a single F&R you must use ‘Replace All’ and not ‘Replace’ (a requirement imposed by use of \K).
Although I haven’t tested it, they should also be fine using Find in Files (or Projects). Observe good backup practices.
If you want actual batching, so that one big operation performs all transformations in one go, you’d need a scipting language solution (that drives a sequence of regex actions from a list). Probably someone else will have a concrete suggestion.