Issue
I want to delete N number of lines after the first match in a text file using sed.
(I know most of these questions have been answered with "use awk", but I want to use sed, regardless of how much more powerful it is than awk. It's more a matter of which tool I'm most comfortable with using at the moment, within a certain time constraint)
The furthest I got is this:
sed -i "0,/pattern/{/pattern/,+Nd}" file.txt
The thought is that 0, denotes the first occurrence, where the curly brackets search the first line for the pattern, and deletes N lines after that occurence
Solution
Try
sed '/pattern/{N;N;N;N;N;N;N;d;}' file.txt
The 0,
construct and the relative line number addressing you tried to use are specific to GNU sed
. Portable sed
does not have these facilities.
This will remove the next six lines after every match. If you only want to remove the first occurrence and leave the rest of the file unchanged, maybe add a separate loop to simply print all remaining lines.
The problem with your attempt is that 0,/pattern/
restricts matching to the lines up through the first occurrence of /pattern/
but then that's the end of the range, so anything selected by this expression cannot operate on lines outside of that range.
Answered By - tripleee Answer Checked By - Katrina (WPSolving Volunteer)