I have several files like file1
, file2
... etc in the same directory and each file may contain several lines matching PATTERN
.
I would like to delete the N
th line from each line matching PATTERN
e.g. with N
= 3
and file1
content like
1 no match
2 PATTERN
3 same PATTERN
4 no match here
5 no match here either
6 another PATTERN
7 again, no match
8 no
9 last line
the expected output is
1 no match
2 PATTERN
3 same PATTERN
4 no match here
7 again, no match
8 no
Editing the files in-place is a bonus, not a requirement (though there's at least one gnu
tool that I know of that could edit them all in one go...)
A similar question was asked here however that is a particular case (there's just a single line matching pattern in each file and the solutions there would only work with multiple lines matching pattern if they're separated by at least N+1 non-matching lines).
tac
, as I suggest in my answer. Congrats on a question that's much trickier than it first appears. :) – Wildcard May 3 '16 at 0:00