Delete the line number 15 in multiple files
-
@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.
-
@PeterJones Thanks for the solution, however, I used (?-s)\A(^.$\R?){14}\K(^.$\R?) to delete line number 1 in multiple files by changing ‘14’ in it to ‘0’ but seems not to work. Can you please help out?
-
@ADEYINKA-ADEWOYIN said in Delete the line number 15 in multiple files:
I used (?-s)\A(^.$\R?){14}\K(^.$\R?) to delete line number 1 in multiple files by changing ‘14’ in it to ‘0’ but seems not to work.
I think you mean you tried:
(?-s)\A(^.*$\R?){0}\K(^.*$\R?)
[BTW, it is better to say exactly what you tried, instead of “I used this but I changed…”. It just helps maximize understanding.]
And while that is a bit of overkill for hitting the first line of a file, in my experiments I found it worked just fine.
Maybe discuss more exactly what you’re doing to achieve the failed result?
-
@Alan-Kilborn How can I delete line number 1 in multiple files?
-
You do realize that @Alan-Kilborn already answered that question for you: the regex he showed worked for him. If it doesn’t work for you, you will have to provide more details , because that regex will work to delete the first line of the files. He even asked you to provide more details to explain how the given regex doesn’t work for you.
You really need to read this FAQ and see the referenes below.
That said, can be simplified: a large part of the original expression was only needed because the original post wanted to delete line#15, not line#1. To delete the first line is simpler: FIND =
(?-s)\A(^.*$\R?)
, REPLACE is empty, and SEARCH MODE must be REGULAR EXPRESSION.----
Useful References