grepはすべての行をフィルタリングして、各ヒットの前に1つを追加します。

grepはすべての行をフィルタリングして、各ヒットの前に1つを追加します。

一部の出力を解析したいので、fooorを含むすべての行とその行のbar直前のすべての行を除外します。たとえば、

echo "
1 some line
2 line to exclude because the next line has one of the terms
3 line with foo
4 line to exclude because the next line has one of the terms too
5 line with bar
6 another line
">InputFile

私は出力が欲しい:

1 some line
6 another line

試してみましたが、cat InputFile|grep -v "foo"|grep -v "bar"2行と4行は除外されず、-B1前の行のオプションも機能しません。

ベストアンサー1

おそらくawk

awk 'BEGIN{a = "foo"}; a !~ /foo|bar/ && $0 !~ /foo|bar/{print a};
{a = $0};END{if(a !~ /foo|bar/){print a}}' InputFile

おすすめ記事