Uhoh , retard alert <| sep |> time for GD to pull his coup d ' etat , become CEO and save YGor Seungri , I ' d be happy either way <| sep |> Well it ' s not entirely clear but it looks like you ' re trying to add a marker around each keyword when found on a line ? Usually the way you would see this done is to build a single regex of all the keywords e . g . word 1 word 2 word 3 When doing that you need to have them in descending order e . g . if you have foofoobar the foobar would never match it would need to be foobarfooAnyways , here ' s some example code which does that use strict use warnings my $ keyword ' keywords . txt ' my $ syslog ' syslog . txt ' my keywords do open my $ fh , '', $ keyword or die could not open $ keyword $! n readline $ fh my $ re sprintf ' b % sb ', join '', sort length $ b length $ a map chomp quotemeta keywords open my $ fh , '', $ syslog or die could not open $ syslog $! n while readline $ fh s $ re $ 1 g print