0

Hi my current code is:

find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*"\
 -daystart -mtime -1 -exec grep -rl "ERROR" "{}" +  | xargs -l  basename

if [ $? -eq 0 ]; then
    tday="$(date +'%d.%m.%Y')"
    echo "ERROR found on $tday in the files obove!"

else
    tday="$(date +'%d.%m.%Y')"
    echo "No ERROR was found at the $tday !"
fi

The code currently outputs the logfiles wich are created or edited this day(not last 24 hrs) and searches if the logfiles contain "ERROR" and simply says in which logfiles is an error or if there aren´t any error he says that aswell.

I censored the names a bit so dont think I messed that up and thats why it doesn´t work ;-)

Output(example):

gBatch_2070.log
gBatch_2071.log
ERROR found on 25.06.2014 in the files obove!

The folder looks like:

folder

Each file looks like:

file

My desired output:

Name of the file + "ERROR" + the message after the error

Example:

gBatch_2067.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet, ...

Thank you in advance for your help!

1

That should be what you search:

find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*" -daystart -mtime -1 \
-exec grep -H "ERROR" {} \; | sed -e 's/.*\/gBatch_/gBatch_/g' -e 's/:[^E]*/: /g' | tr '\n' ', '

Example ouput:

gBatch_2070.log:ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet
gBatch_2070.log:ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet
gBatch_2071.log:ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet
...

Explanation:

  • -H forces grep to print the filename too
  • sed 's/.*\/gBatch_/gBatch_/g' make the filename to the base filename
  • Does your version replace my whole script or just the find? When I only execute the find, I get the error´s each in one line and with the unwanted info (:2014-06-05 06:40:45,483 [main]) – BlueFox Jun 25 '14 at 11:26
  • @BlueFox Replaces the whole script. I changed the part, that is only one line and without the unneeded info – chaos Jun 25 '14 at 11:41
1
find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*"\
 -daystart -mtime -1 -exec grep -rl "ERROR" "{}" +  | xargs -l  basename\
 > /tmp/files_found

if [ $? -eq 0 ]; then
    tday="$(date +'%d.%m.%Y')"

    while read line
    do
       error=`grep "ERROR" /home/user/logfilesError/$line`
       error=`echo $error | sed 's/^.*ERROR/ERROR/' | tr '\n' ', '`
       echo "$line - $error"
    done < /tmp/files_found

    echo "ERROR found on $tday in the files obove!"
    rm /tmp/files_found

else
    tday="$(date +'%d.%m.%Y')"
    echo "No ERROR was found at the $tday !"
fi
  • 1
    My bad - The grep should not have still had the -rl parameters when it was used the second time. – Warwick Jun 25 '14 at 8:13
  • Nice it looks great now! Is it possible to cut the "2014-06-05 07:05:45,274 [main]" between the logfile and the error message away? – BlueFox Jun 25 '14 at 8:15
  • 1
    Can you show me the output? It is a bit hard to tell exactly what needs to be done there. – Warwick Jun 25 '14 at 8:29
  • Current output: gBatch_2071.log - 2014-06-05 07:05:45,274 [main] ERROR *****.batch.batch.BatchStart - Batchverarbeitung beendt. ERROR found on 25.06.2014 in the files obove! – BlueFox Jun 25 '14 at 8:29
  • 1
    Try now with the extra sed line. – Warwick Jun 25 '14 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.