in reply to Perl file editing

You crossposted the question to StackOverflow without telling us (well, if we don't take the 0 down vote favorite part as a hint). Crossposting is allowed, but you should inform us about the fact to save people not attending both sites from spending their time hacking a solution to the problem already solved at the other end of the internet.

Also, instead of creating a new version of a question, you should edit the original one. Read How do I change/delete my post? for details.

To show the neighbouring lines, you can use a "sliding window", i.e. always keep three lines in memory. If the middle one matches the criterion, show the other two and ask the user for confirmation.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ