Delete the line number 15 in multiple files
-
i tried replacing it, but i have other similar lines in same file,
so is there a way to delete/replace exactly the line number 15. -
If you are saying, “I want to delete every line in all these files that contains start-of-line, 4 spaces, the digit two, and a comma, then end of line”, that’s easy: FIND =
^\x20{4}2,\R
(there were 4 spaces between the ^ and the 2, but the forum collapsed them, so I edited the post to use the regex-equivalent\x20{4}
), REPLACE = (make this box empty), SEARCH MODE = Regular ExpressionIf you are saying, “I want to grab the contents of line 15 in my first file, then delete that line, and any line that matches that same text in any of the other files”: that cannot be done with find-in-files. The regex engine doesn’t have any “memory” across files, so it cannot know what line 15 looked like from the first file.
If you are saying, “I want to delete just the 15th line in every file, no matter what the contents”, then that’s doable: FIND =
(?-s)\A(^.*$\R?){14}\K(^.*$\R?)
, REPLACE = (make this box empty), SEARCH MODE = Regular Expression.----
Useful References
-
@senpay98k said in Delete the line number 15 in multiple files:
i tried replacing it, but i have other similar lines in same file,
so is there a way to delete/replace exactly the line number 15.Does the work exactly as writen, thanks a lot :)
-
“I want to delete just the 15th line in every file, no matter what the contents”
FIND:
\A(?:^.*$\R){14}\K.*$
-
@robin-cruise said in Delete the line number 15 in multiple files:
“I want to delete just the 15th line in every file, no matter what the contents”
FIND: \A(?:^.$\R){14}\K.$That’s extremely similar to the @PeterJones solution.
Do you have anything to add to explain why your solution meets the need any better, or why its difference is significant? -
Do you have anything to add to explain why your solution meets the need any better, or why its difference is significant?
Of course is a similar solution, except my regex is shorter. It is an alternative solution.
-
@robin-cruise said in Delete the line number 15 in multiple files:
except my regex is shorter
Shorter regexes aren’t always better.
In fact, the regex engine has to work harder to find a match with your version.
So I’d suggest not answering a solved issue a week later with just something shorter. -
So I’d suggest not answering a solved issue a week later with just something shorter.
as if to tell a car manufacturer not to produce small cars anymore, because it already has big cars …
I believe that any alternative should be encouraged
-
@robin-cruise said in Delete the line number 15 in multiple files:
believe that any alternative should be encouraged
Right, especially if it slows down processing. OMG.
-
For future readers: the solution proposed by @robin-cruise does not do the same thing as my solution. So depending on your definition of “delete the line number 15”, that “alternate” solution will fail.
Given the data
one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen
the “alternate” solution
\A(?:^.*$\R){14}\K.*$
will do one of two things: if you have. matches newline
enabled, it will match the zero-width match at the very end of the file, deleting nothing, which is obviously not what is desired, by any interpretation of “delete line number 15”. If you have. matches newline
unchecked, it will delete the contents of line 15, but still leave the newline:one two three four five six seven eight nine ten eleven twelve thirteen fourteen sixteen
… with a gap (blank line) between fourteen and sixteen. This may not be what is desired.
On the other hand, my expression
(?-s)\A(^.*$\R?){14}\K(^.*$\R?)
will delete the whole line, including the newline sequence, regardless of the, matches newline
setting.one two three four five six seven eight nine ten eleven twelve thirteen fourteen sixteen
This leaves no blank line between
fourteen
andsixteen
.Since the OP has already said that my solution did exactly what was desired for this question, having someone else providing an answer which results in anything different from the desired results is not presenting an alternative solution, it is presenting an incorrect “solution”, at least for this question. It may be useful to someone who does want to leave the blank line in line 15. but it’s not what the OP wanted.