Please add some example input and output. I think I know what you're trying to do; and it would be easy to do with awk, but I'd like to make sure I'm clear on what you want before I post an answer.
–
HalosGhostAug 26 '14 at 22:05

2

Welcome to U&L, please try and include some example data of what you're asking for. It's difficult to parse what you want w/o it, as is evidence in the A'ers you're receiving. They all work slightly different and are making this a bit of a messy Q&A.
–
slm♦Aug 27 '14 at 1:26

5 Answers
5

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.

The main pattern never appears on the last line of the input, per the question.
–
Michael HomerAug 27 '14 at 1:31

Where did you read that in the main Q? I'm missing that condition.
–
slm♦Aug 27 '14 at 1:40

"The next line always appears after the line with the pattern match" implies that there must be a following line, and so the main pattern can't ever occur on the last line.
–
Michael HomerAug 27 '14 at 1:48

We'll wait for the OP, b/c I didn't not read that line at all like that. As I mentioned in steeldriver's A. Oh well, we wait, no biggie.
–
slm♦Aug 27 '14 at 1:51

However the question says [t]he next line always appears after the line with the pattern match
–
steeldriverAug 27 '14 at 1:18

Yeah this Q is poorly written IMO. I've read that statement 10 times and do not understand what it's actually saying. I think he needs to include sample data and clean it up or we should just close it.
–
slm♦Aug 27 '14 at 1:24

Most seds do not print the last line if you N on $. But GNU sed will. So if the pattern you want deleted is on the last line and you N it will print. It is sometimes better to keep the buffer full - as in, always holding 2 lines except on the lines you do not want to print. You might do so like:

seq 10 | sed -n 'x;/7/!g;//!p'

That's an example with seq as input. On every line it swaps hold space and pattern space. If the last held line does not match a 7(in this case) it will overwrite the hold space with the current line. It then checks again that the line it just pulled in - the current line - also does not match the 7, otherwise it will not print it. So on every line it checks the previous and the current line.

1
2
3
4
5
6
9
10

And if your pattern does fall on the last line:

seq 10 | sed -n 'x;/10/!g;//!p'
1
2
3
4
5
6
7
8
9

Another example, to hopefully demonstrate more clearly what it will and will not print:

Line 1 looks for lines matching the pattern, sets a flag, and skips the line. Line 2 skips a line when the flag is set, but resets the flag. Line 3 prints lines that weren't skipped by one of the other two lines.