不要な行を削除する方法は? 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また、ファイル名やパスに関係なく関数を作成しました。唯一の目的は、入力ストリームを取得してフィルタ処理されたストリームを出力することです。

おすすめ記事