I have gone through stack over flow and found these questions
How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?
Combine multiple lines between flags in one line in AWK
The problem with my question is that there can be another TAG1 without the matching TAG2 like this
file.txt:
aa
TAG1
some right text
TAG2
some text2
TAG1
some text3
TAG1
some text4
TAG1
some right text 2
TAG2
some text4
TAG1
some text5
some text6
expected output:
TAG1
some right text
TAG2
TAG1
some right text 2
TAG2