The history of transactions done on an entity within our systems look like below:
1 BYM1 TSTAB 09NOV 0035 CAB
Sometext 01
2 BYM1 TSTAB 09NOV 0035 CAB
Can be done - question
3 BYM1 TSTAB 09NOV 0035 CAB
Sometext 02
Sometext 03
6 BYM3 TSTAA 09NOV 0400 CAA
Some 04 text 04
7 BYM3 TSTAA 10NOV 0455 CAC
Sometext 06
Sometext 06 line 2
8 BYM3 TSTAA 10NOV 0455 CAC
Sometext 07
9 BYM2 TSTAC 10NOV 0619 CAD
Some 08 text 0008 ABCD
Some 08 text 0008 BB00
Some 08 text 0008 CC00
Some 08 text 0008 DD00
Some 08 text 0008 EE00
10 BYM2 TSTAC 10NOV 0627 CAD
Something BBBBBSSDGFSDSF
11 BYM2 TSTAC 10NOV 0627 CAD
Something else
12 BYM2 TSTAC 10NOV 0627 CAD
What text here
13 BYM4 TSTAC 10NOV 0711 CAD
Tired figuring out
19 BYM3 TSTAA 11NOV 0438 CAE
Some 04 text 05 05 05
20 BYM3 TSTAA 11NOV 0441 CAF
Not so confidential now
21 BYM3 TSTAA 11NOV 0441 CAF
Some 00 text 0009 X1X2
43 BYM3 TSTAA 11NOV 0441 CAD
Some 0A text 0009 ABCD
44 BYM3 TSTAA 11NOV 0441 CAD
Some 1B text
45 BYM3 TSTAA 12NOV 1455 CAC
Something 0AADDBB
8782 BYM3 TSTAA 12NOV 1610 CAD
Something 0AADDBB
8830 BYM3 TSTAA 12NOV 1612 CAA
Something 0AADDBB
9999 BYM3 TSTAA 12NOV 1722 CAA
Something 0AADDBB
The blocks of text start with the line which has a number in the first 4 characters. (The number is actually a running sequence number and every transaction is indexed with that). The category (of the transaction) of the block is defined by the last three characters in the line which has the number.
I am looking for a awk, sed (, vi, grep) script to search for blocks of text belonging to a "category", sort the resultant blocks in descending order of the index (numbers), display the number of blocks I have asked for.
For example, if I want to search of 4 blocks of category "CAD" the output I would like to see is:
8782 BYM3 TSTAA 12NOV 1622 CAD
Something 0AADDBB
44 BYM3 TSTAA 11NOV 0441 CAD
Some 1B text
43 BYM3 TSTAA 11NOV 0441 CAD
Some 0A text 0009 ABCD
13 BYM4 TSTAC 10NOV 0711 CAD
Tired figuring out
How can I acheive this. Any help would be greatly appreciated :-)