SED to print from [first occurance] to [last occurance]

Dear Experts,

I have a 84K rows file of which I need to filter out certain data.
I would need all lines starting from a string to the last occurence of string but including the first occurrence of [Total] after that last ocurrence...

File would look like this:

xxxxxxx
xxxxxxxxxx
xxx
xx
xxxx

FIRST OCCURENCE OF STRING [AAA-111]  
 adfsdf
sdfsdd
TOTAL

SECOND OCCURENCE OF STRING [AAA-111]
sfdsfsdfdfd
sdf

sdfffsf
TOTAL

THIRD OCCURENCE OF STRING [AAA-111]
dfsfsdaf

LAST OCCURENCE OF STRING [AAA-111]
sdfsdf
sdfsd

sdfsd
sdfsdf

sdfsd

TOTAL

xxxxxxxxxxx
xxxxxxx
xxxxxxxxxxxx

xxxxxxxxxxxx


So I would need everything that is between the x's
The first occurence and the last occurence are grouped.

Hope you can help.

WatnogAsked:
Who is Participating?
 
medveddCommented:
Yes, it could be any regular expression between //. For UTLAPPP01 and UTLAPPP03:

sed -n '/UTLAPPP0[13]/,/Total/p' example.txt
0
 
shajithchandranCommented:
couldnt understand the question.. can you give some more details.. with examples
0
 
WatnogAuthor Commented:
See file attached.
What syntax would be needed to extract for instance all data concerning UTLAPPP03.
Thanks.
example.txt
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
medveddCommented:
sed -n '/UTLAPPP03/,/Total/p' example.txt
0
 
WatnogAuthor Commented:
Thank you! That works great. Could different search strings be combined, let's say look for UTLAPPP03 but also for UTLAPPP01?
0
 
WatnogAuthor Commented:
Great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.