I am trying to build a script that will check for multiple patterns in a log file and will send the matching lines plus more details of the error by email (i.e, . the email should contain, 10 lines before the pattern matching lines + pattern matching line + 10 lines after the pattern matching line).

I have started building my script as below. I am new in perl and getting few obstracles and will appreciate if any of you kindly help me to finish this.

Here are my problems:

P:1

Original log file name = abcEarlyCheckout.out.2014.01.17T04.35.02.log This log file will be produced once daily but I am unable to assign log with with $todaydate variable plus the '*' as perl does not recognize '*'.

Trying to assign the log file in the script as: $log="abcEarlyCheckout.out.2014.01.17.$todaydate.*.log";

P:2 Now that I am able to search the pattern and get the error line, how can I include pre and post matching pattern lines (10 lines before the pattern matching lines + pattern matching line + 10 lines after the pattern matching line) in my logic so that the email will have the detail of the error lines?

Your script has many other issues that need fixing, but I don't want to overwhelm you with a major critique.

There are multiple ways to get the range of lines before and after the matching line. The most common would be to load the file into an array and loop over the array. Doing that will make it easy to output the 10 array elements before and after the match.

Hmm, not entirely sure what you are trying to do, but the second line in the code above does not make sense: when you are trying this regex match, you still haven't read any line from the file, so this is doomed to fail (in addition to the syntax that most probably does not work).

You should probably slurp your file into the @lines array, and then go through the array to look for a match. When you get a match, then you can print the lines before and after the line where the match occurred.