The trick is to store the last line seen in the "hold space".
sed -n '
/^xxxx/{n
n
x
d
}
x
1d
p
${x
p
}
' <input file>
Starting with the x
- swap the current input line with the hold space (x
), then for the first line don't print anything (1d
), subsequent lines print the line just swapped from the hold space (p
), on the last line swap the hold space again and print what was in it ($x{x p}
. That leaves what to do when we hit the target line (starting /^xxxx/
) - read the next two lines into the pattern space (n n
) and swap the pattern space with the hold space (x
) - this leaves the hold space with next line we want to print and the pattern space with the line before the match, which we don't want, so we ditch it (d
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…