You said each entity in the file is composed of 4 lines but the second one in your example only has 3 lines..
Do you have code/thoughts you've tried so far?
one method would be to read 4 lines at a time. if the 4th line matches, print the first line. Another method would be to set $/ to '# input' and then regex the resulting 'lines' (or grep all of them).