でエミュレートする方法grep -B1はsed次のとおりです。
sed '$!N;/pattern/P;D' infile
これはこちらのものとよく似ています。もう1つと同様に、Next行で読み取りますが、今回は、パターンスペースが一致する場合、P最初の\newline までリントし、次に、もう1つと同様に、最初のewline まで削除して、サイクルを再開します。したがって、サンプル入力では:  D\n
sed '$!N;/&/P;D' infile
出力:
aaaaaaaaa
bbbbbbbbb &
ccccccccc &
eeeeeeeee
fffffffff &
ggggggggg &
繰り返しますが、それがどのように機能するかを確認するために、l前後にを追加Nして、パターンスペースを確認します。
sed 'l;$!N;l;/&/P;D' infile
たとえば、サンプルファイルの場合:
zzzz &
aaaa
bbbb
cccc &
dddd &
hhhh
eeee
ffff &
gggg &
これらは、sed実行するコマンドと対応する出力です。
cmd出力cmd
l       zzzz &$               N # read in the next line
l       zzzz &\naaaa$         # pattern space matches so print up to \n
P       zzzz &                D # delete up to \n 
l       aaaa$                 N # read in the next line
l       aaaa\nbbbb$           D # delete up to \n (no match so no P)
l       bbbb$                 N # read in the next line
l       bbbb\ncccc &$         # pattern space matches so print up to \n
P       bbbb                  D # delete up to \n
l       cccc &$               N # read in the next line
l       cccc &\ndddd &$       # pattern space matches so print up to \n
P       cccc &                D # delete up to \n
l       dddd &$               N # read in the next line
l       dddd &\nhhhh$         # pattern space matches so print up to \n
P       dddd &                D # delete up to \n
l       hhhh$                 N # read in the next line
l       hhhh\neeee$           D # delete up to \n (no match so no P)
l       eeee$                 N # read in the next line
l       eeee\nffff &$         # pattern space matches so print up to \n
P       eeee                  D # delete up to \n
l       ffff &$               N # read in the next line
l       ffff &\ngggg &$       # pattern space matches so print up to \n
P       ffff &                D # delete up to \n
l       gggg &$               # last line so no N 
l       gggg &$               # pattern space matches so print up to \n
P       gggg &                D # delete up to \n