##
perl -ne 'BEGIN{$re=qr/foo/}if(/$re/){print$p if$p!~/$re/;print$_};$p=$_' file
####
grep -B 1 foobar file
####
perl -ne '$i=$.% 2; $a[$i]=$_; print @a[$i+1..$#a,0..$i] if /foo/' file
perl -ne '$i=$.% 2; $a[$i]=$_; print @a[$i+1..$#a,0..$i] and @a=() if /foo/' file