원하지 않는 줄을 제거하는 방법은 무엇입니까? 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,/^[0-9]/1부터 숫자( )로 시작하는 첫 번째 줄(포함)까지의 모든 줄에 적용됩니다.

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]

관련 정보