I have few processes in the server continuous run few jobs, each of the process will generate a log file which detailing when its jobs are completed.
the logfile will has the name something like this, result1.log, result2.log, result3.log,.... result10.log, result11.log, result12.log.......
I want to grep all the logfiles with the keyword "completed", the line of the "completed" keyword will also has the date/time info, this will shows me when was the jobs are completed, also I want to sort the output based on logfile name in order, e.g. result1.log, result2.log, result3.log, result4.log etc... and then send me an email.
Anyone help me to construct the script will great appreciate.
Because your filenames don't readily sort in order (as result2.txt appears to come after result10.txt but should come before it) you'll need to either teach the sort command to understand your name or explicitly list them all in the right order.
Once you have that all in some variable (say $FILES for example), you just need to grep them and pipe the output into mail.
eg
for file in $FILES
do
grep "completed" $file
done | mail -s 'All these things are completed!' me@myemailaddress.com
You could run grep on the list directly, but then you've got to suppress grep's default behaviour of listing the filename before each output line.
The output10.log, output11.log, output12.log are comes out after output1.log, how could I sort the logfiles in the order where output10.log, output11.log and output12.log etc are come out after output9.log?