I'd like to extract the content between two strings using sed in UNIX. I know that sed only works on one line but in my case, I'd like to extract content that stretches over multiple lines.
To give you an idea of the situation, here's a string variable with lines I'd like to extract and lines I'd like to ignore:
VAR="Unrelevant
Unrelevant
Title1
Relevant
Relevant
Title2
Unrelevant"
Now I'd like to extract the part between "Title1" and "Title2" including "Title1" and save the content into the variable called "RELEVANT". Using echo on that variable should be give me this desired output:
Title1
Relevant
Relevant
My attempt was:
RELEVANT=(echo "$VAR" | sed -e 's/.*Title1\(.*\)Title2.*/\1/g'))
But this partly worked only when VAR was a one-liner:
VAR="Unrelevant Unrelevant Title1 Relevant Relevant Title2 Unrelevant"
"Partly", because the output was:
Relevant Relevant
... with a space before the first "Relevant", which isn't supposed to be there and without the preceding "Title1".
Well, apart from this, I can't get this working if VAR goes over multiple lines. So my question: How do I get this working for a string which stretches multiple lines?