Inputfile.txt is given file and output.txt is expected output in attachment.

In this situation we have to deal with nested case statements, which you can observe by seeing the input file and output given in attachment.
I got the output for one level nested case statement. first I saved the content between first "case" and last "esac" in a temp file and run the below given code.
Code:
open (data,"<input.txt");
...