Produza várias strings de vários arquivos

Produza várias strings de vários arquivos

Olá, meu código atual é:

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

O código atualmente exibe os arquivos de log que foram criados ou editados neste dia (não nas últimas 24 horas) e pesquisa se os arquivos de log contêm "ERRO" e simplesmente diz em quais arquivos de log há um erro ou se não há nenhum erro ele diz isso também.

Eu censurei um pouco os nomes, então não pense que estraguei tudo e é por isso que não funciona ;-)

Saída (exemplo):

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

A pasta se parece com:

pasta

Cada arquivo se parece com:

arquivo

Minha saída desejada:

Nome do arquivo + “ERROR” + mensagem após o erro

Exemplo:

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

Agradeço antecipadamente por sua ajuda!

Responder1

Deve ser isso que você pesquisa:

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

Exemplo de saída:

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

Explicação:

  • -Hforça o grep a imprimir o nome do arquivo também
  • sed 's/.*\/gBatch_/gBatch_/g'transforme o nome do arquivo no nome do arquivo base

Responder2

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

informação relacionada