Using awk
or sed
how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns.
For example: Suppose the file contains:
abc
def1
ghi1
jkl1
mno
abc
def2
ghi2
jkl2
mno
pqr
stu
And the starting pattern is abc
and ending pattern is mno
So, I need the output as:
def1
ghi1
jkl1
def2
ghi2
jkl2
I am using sed to match the pattern once:
sed -e '1,/abc/d' -e '/mno/,$d' <FILE>
Is there any way in sed
or awk
to do it repeatedly until the end of file?
ベストアンサー1
awk
必要に応じてフラグを使用して印刷をトリガーします。
$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file
def1
ghi1
jkl1
def2
ghi2
jkl2
これはどのように作動しますか?
/abc/
同様、このテキストを含む行に一致します/mno/
。/abc/{flag=1;next}
flag
テキストが見つかったときにを設定しますabc
。その後、その行をスキップします。/mno/{flag=0}
flag
テキストが見つかったときにを設定解除しますmno
。- 最後は
flag
、デフォルトのアクションを持つパターンです。 が1 に等しいprint $0
場合は、その行が印刷されます。flag
より詳しい説明と例、およびパターンが表示される場合と表示されない場合については、2 つのパターン間の線を選択するにはどうすればよいですか?。