特定の単語を含まない2つのパターンの間にテキストを印刷します。

特定の単語を含まない2つのパターンの間にテキストを印刷します。

特定の単語を含まない2つのパターンの間にいくつかのテキストを印刷したいと思います。

入力テキストは

HEADER asdf asd 
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs

必要な出力は次のとおりです。

HEADER asdf asd
asd COW assd
TAIL sdfsdfs

HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs

概念的には、これが必要です。

awk '/HEADER/,!/DOG/,TAIL' text 

ベストアンサー1

そしてperl

perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?\n/mgs' your-file

そしてawk

awk '! inside {if (/^HEADER/) {inside = 1; skip = 0; content = ""} else next}
     /DOG/{skip = 1; next}
     ! skip {content=content $0 "\n"}
     /^TAIL/ {if (!skip) printf "%s", content; inside = 0}' your-file

おすすめ記事