Generar múltiples cadenas a partir de múltiples archivos

Generar múltiples cadenas a partir de múltiples archivos

Hola mi código actual es:

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

Actualmente, el código genera los archivos de registro que se crearon o editaron este día (no las últimas 24 horas) y busca si los archivos de registro contienen "ERROR" y simplemente dice en qué archivos de registro hay un error o si no hay ningún error, también dice eso.

Censuré un poco los nombres, así que no crean que lo arruiné y por eso no funciona ;-)

Salida (ejemplo):

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

La carpeta se parece a:

carpeta

Cada archivo se parece a:

archivo

Mi resultado deseado:

Nombre del archivo + "ERROR" + el mensaje después del error

Ejemplo:

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

¡Gracias de antemano por su ayuda!

Respuesta1

Eso debería ser lo que buscas:

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' ', '

Salida de ejemplo:

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
...

Explicación:

  • -Hobliga a grep a imprimir el nombre del archivo también
  • sed 's/.*\/gBatch_/gBatch_/g'hacer que el nombre del archivo sea el nombre del archivo base

Respuesta2

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

información relacionada