hold buffer is good for storing a line (or group of lines) until some later test proves true. In other words, it is good for handling sequences of data which you want sequential but are not yet sequential - because it enables you to stick them together. But it also requires a lot of copies between the two buffers. This isn't so bad if you're building up a series of lines with
Hold commands - just appending - but every time you e
xchange buffers you copy the whole of one to the other and vice-versa.
When you're working with a series of lines which are already sequential, and you want to prune them based on context, then the better way to go is with
look-ahead - as opposed to the
hold-buffer's look-behind. cuonglm does this for the second half of his answer already - but you can use that logic for either form.
sed '$!N;/\nage.*: 10/P;D' <infile >outfile
See, that will append the
Next input line following an embedded
\newline delimiter to the current pattern space on every line which is
$last. It then checks if the line just pulled matches a pattern, and, if so it
Prints only up to the first
\newline in pattern space - so only the preceding line. Last, it
Deletes up to the first
\newline in pattern space and starts the cycle again. So throughout the file you maintain a one-line look-ahead without unnecessarily swapping buffers.
If I alter the command only a little you can see specifically how it works - by sliding over the file with a two-line window throughout. I'll add a
look command just before the
sed '$!N;/\nage.*: 10/P;l;D'
Name is : sara
Name is : sara\nage is : 10$
age is : 10\nName is : john$
Name is : john\nage is : 20$
age is : 20\nName is : Ron$
Name is : Ron
Name is : Ron\nage is : 10$
age is : 10\nName is : peggy$
Name is : peggy\nage is : 30$
age is : 30$
That's its output. The lines which end in
$ are the result of the
look command - which renders an escaped version of pattern space to stdout. The lines which do not end in
$ are those which would otherwise be
Printed. As you can see, the previous line is only
Printed when the second line in pattern space - the
Next line as just pulled in and which follows the
\newline in pattern space - matches your pattern.
Besides the solutions already offered you, another way you might go for printing only Name lines preceding an age line which does not end in 10:
sed -n '/^Name/N;/ 10$/!s/\nage.*//p'
...which only appends a
\newline followed by the
Next input line if pattern space begins with the string Name, and only
prints a line to output if pattern space does not end with the string 10 and if
sed can successfully
s///ubstitute away a
\newline followed by the string age and all that follows until the tail of pattern space. Because there cannot be a
\newline in pattern space except as the result of an edit command - such as
Next - the ensures that the only Name lines printed are those immediately preceding an age line which does not end in the string 10.
All of the syntax used in the above answer is POSIX standard - it should work as written with any
sed which supports the standard.