マッチした前の行の表示 (続き)


これらにはいろんな書き方がありますけど、以下のようなものも投稿されています。

/string/{print a[NR%3] RS a[(NR+1)%3] RS a[(NR+2)%3]}
{a[NR%3]=$0}
/string/{
    line_3=a[NR%3]; line_2=a[(NR+1)%3]; line_1=a[(NR+2)%3]; line_0=$0
    # now you have the current line in variable line_0, the preceeding
    # lines in line_1, etc., and you can print them or do whatever
}
{a[NR%3]=$0}