I am trying to remove all instances of a pattern match from a file if it matches a pattern. If there is a match, the (complete) line with the matching pattern and the next line get removed. The next line always appears after the line with the pattern match, but in addition it appears in other areas of the file.I am using grep and it is deleting all occurrences of the next line in the file, as expected.Is there a way I can remove that next line if and only if it is after the line with the pattern match?
Remove multiple lines after a pattern match but only after pattern match
sed;grep
You can use sed with the N and d commands and a {} block:sed -e '/pattern here/ { N; d; }'For every line that matches pattern here, the code in the {} gets executed. N takes the next line into the pattern space as well, and then d deletes the whole thing before moving on to the next line. This works in any POSIX-compatible sed.