tac somefilename | perl -ne 'print if /PATTERN3/../PATTERN1/' | tac