これは、N、P、Dコマンドを使いこなす例です。おそらく、最も習得が難しいものです。
#!/usr/bin/sed -f h
:b
# On the last line, print and exit
$b
N
/^\(.*\)\n\1$/ {
# The two lines are identical. Undo the effect of
# the n command.
g
bb
}
最終行では出力して終了します。2行が同じ場合は、nコマンドの効果を元に戻します。
# If the N command had added the last line, print and exit
$b
Nコマンドが最終行を追加した場合は、出力して終了します。
# The lines are different; print the first and go # back working on the second. P D
行が異なる場合は、最初の行を出力し、2行目の処理へ戻ります。
ご覧のとおり、PとDで2行分の窓を保持しています。この手法は、高度なsedスクリプトでよく使われます。