como se livrar de linhas indesejadas? enquanto loop?

1

Eu escrevi este código:

#!/bin/bash
filter(){

grep -Ev "INFO" /app/me/logs/$1

}

filter "$1" > /app/me/logs/${1}.filtered

cat /app/me/logs/${1}.filtered

então o que ele faz é que elimina todas as linhas que tem a palavra INFO nele. Mas, ao mesmo tempo, está produzindo o seguinte:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 27, 2016 2:55:09 PM org.apache.coyote.AbstractProtocol start
Sep 27, 2016 2:55:09 PM org.apache.coyote.AbstractProtocol start
Sep 27, 2016 2:55:09 PM org.apache.catalina.startup.Catalina start
16:53:05,024 ERROR [liferay/scheduler_dispatch-6][JDBCExceptionReporter:82] ORA-00001: unique constraint (LIFERAY.IX_57D82B06) violated_ [Sanitized]
16:53:05,028 ERROR [liferay/scheduler_dispatch-6][JDBCExceptionReporter:82] ORA-00001: unique constraint (LIFERAY.IX_57D82B06) violated_ [Sanitized]
16:53:05,123 ERROR [liferay/scheduler_dispatch-6][ParallelDestination:72] Unable to process message {destinationName=liferay/scheduler_dispatch, response=null, responseDestinationName=null, responseId=null, payload=null, values={JOB_NAME=com.liferay.portlet.trash.messaging.CheckEntryMessageListener, EXCEPTIONS_MAX_SIZE=0, GROUP_NAME=com.liferay.portlet.trash.messaging.CheckEntryMessageListener, DESTINATION_NAME=liferay/scheduler_dispatch, JOB_STATE=com.liferay.portal.kernel.scheduler.JobState@3c5ee4ef, companyId=0, MESSAGE_LISTENER_CLASS_NAME=com.liferay.portlet.trash.messaging.CheckEntryMessageListener, RECEIVER_KEY=com.liferay.portlet.trash.messaging.CheckEntryMessageListener.com.liferay.portlet.trash.messaging.CheckEntryMessageListener, MESSAGE_LISTENER_UUID=6f44c739-1146-447a-893d-16099898c2ff}}
com.liferay.portal.kernel.messaging.MessageListenerException: com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:32)
    at sun.reflect.GeneratedMethodAccessor262.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
    at com.sun.proxy.$Proxy296.receive(Unknown Source)
    at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:76)
    at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:72)
    at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:69)
    at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:682)
    at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:593)
    at java.lang.Thread.run(Thread.java:744)
Caused by: com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.processException(BasePersistenceImpl.java:251)
    at com.liferay.portal.service.persistence.GroupPersistenceImpl.fetchByC_C_C(GroupPersistenceImpl.java:5333)
    at com.liferay.portal.service.persistence.GroupPersistenceImpl.fetchByC_C_C(GroupPersistenceImpl.java:5245)
    at com.liferay.portal.service.persistence.GroupPersistenceImpl.findByC_C_C(GroupPersistenceImpl.java:5205)
    at com.liferay.portal.service.impl.GroupLocalServiceImpl.getCompanyGroup(GroupLocalServiceImpl.java:1091)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

Como você pode ver, funciona bem, mas há algumas linhas não-prontas na parte superior que não contêm palavras como "ERROR | FATAL | WARN". Eu não preciso dessas linhas, mas ao mesmo tempo eu quero as linhas que são recuadas e a linha anterior. Eu quero que seja algo parecido com:

16:53:05,024 ERROR [liferay/scheduler_dispatch-6][JDBCExceptionReporter:82] ORA-00001: unique constraint (LIFERAY.IX_57D82B06) violated_ [Sanitized]
16:53:05,028 ERROR [liferay/scheduler_dispatch-6][JDBCExceptionReporter:82] ORA-00001: unique constraint (LIFERAY.IX_57D82B06) violated_ [Sanitized]
16:53:05,123 ERROR [liferay/scheduler_dispatch-6][ParallelDestination:72] Unable to process message {destinationName=liferay/scheduler_dispatch, response=null, responseDestinationName=null, responseId=null, payload=null, values={JOB_NAME=com.liferay.portlet.trash.messaging.CheckEntryMessageListener, EXCEPTIONS_MAX_SIZE=0, GROUP_NAME=com.liferay.portlet.trash.messaging.CheckEntryMessageListener, DESTINATION_NAME=liferay/scheduler_dispatch, JOB_STATE=com.liferay.portal.kernel.scheduler.JobState@3c5ee4ef, companyId=0, MESSAGE_LISTENER_CLASS_NAME=com.liferay.portlet.trash.messaging.CheckEntryMessageListener, RECEIVER_KEY=com.liferay.portlet.trash.messaging.CheckEntryMessageListener.com.liferay.portlet.trash.messaging.CheckEntryMessageListener, MESSAGE_LISTENER_UUID=6f44c739-1146-447a-893d-16099898c2ff}}
com.liferay.portal.kernel.messaging.MessageListenerException: com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:32)
    at sun.reflect.GeneratedMethodAccessor262.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
    at com.sun.proxy.$Proxy296.receive(Unknown Source)
    at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:76)
    at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:72)
    at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:69)
    at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:682)
    at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:593)
    at java.lang.Thread.run(Thread.java:744)
Caused by: com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.processException(BasePersistenceImpl.java:251)
    at com.liferay.portal.service.persistence.GroupPersistenceImpl.fetchByC_C_C(GroupPersistenceImpl.java:5333)
    at com.liferay.portal.service.persistence.GroupPersistenceImpl.fetchByC_C_C(GroupPersistenceImpl.java:5245)
    at com.liferay.portal.service.persistence.GroupPersistenceImpl.findByC_C_C(GroupPersistenceImpl.java:5205)
    at com.liferay.portal.service.impl.GroupLocalServiceImpl.getCompanyGroup(GroupLocalServiceImpl.java:1091)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

Qualquer ajuda? Obrigado,

    
por Abdallah Qaraeen 02.11.2016 / 19:34

2 respostas

0

Para filtrar todas as linhas, até a primeira linha que começa com um dígito, use o GNU sed da seguinte forma:

sed '1,/^[0-9]/{/^[0-9]/!d}' logfile

Este script de edição de sed aplicará /^[0-9]/!d , ou seja, "excluir todas as linhas que não iniciam com um dígito", em todas as linhas no intervalo 1 até a primeira linha que começa com um dígito ( 1,/^[0-9]/ ), inclusive.

Para filtrar adicionalmente qualquer linha INFO:

sed -e '1,/^[0-9]/{/^[0-9]/!d}' -e '/INFO/d' logfile

Então seu script se torna

#!/bin/bash

function filter {
    sed -e '1,/^[0-9]/{/^[0-9]/!d}' -e '/INFO/d'
}

filter <"/app/me/logs/$1" >"/app/me/logs/$1.filtered"

Repare também que eu fiz a função filter agnóstico de nomes de arquivos ou caminhos. Sua única finalidade é pegar um fluxo de entrada e gerar um fluxo filtrado.

    
por 05.01.2017 / 12:58
-1

Por que não o seguinte:

grep -Ev "INFO|SLF4J|201[0-9]
    
por 02.11.2016 / 20:51

Tags