如何去掉不需要的线条? while 循环?

如何去掉不需要的线条? while 循环?

我写了这段代码:

#!/bin/bash
filter(){

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

}

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

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

所以它的作用是消除所有包含 INFO 一词的行。但同时它输出以下内容:

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)

正如您所看到的,它工作正常,但顶部有一些不需要的行,不包含诸如“ERROR|FATAL|WARN”之类的单词。我不需要这些行,但同时我想要缩进的行及其前面的行。我希望它看起来像:

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)

有什么帮助吗?谢谢,

答案1

要过滤掉所有行,直到以数字开头的第一行,请sed像这样使用 GNU:

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

sed编辑脚本将/^[0-9]/!d“删除所有不以数字开头的行”,应用于从 1 到以数字 ( 1,/^[0-9]/) 开头的第一行(含)范围内的所有行。

要另外过滤掉任何 INFO 行:

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

所以你的脚本变成

#!/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"

还要注意的是,我使filter函数与文件名或路径无关。它的唯一目的是获取输入流并输出过滤后的流。

答案2

为什么不是以下内容:

grep -Ev "INFO|SLF4J|201[0-9]

相关内容