我如何读取多个文件

我如何读取多个文件

文件1:

at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:940)
        at com.sun.corba.se.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:198)
        at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:712)
        at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.dispatch(SocketOrChannelConnectionImpl.java:469)
        at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.doWork(SocketOrChannelConnectionImpl.java:1230)
        
    РЯа. 19, 2021 4:14:16 PM com.lhs.ccb.sfw.application.ServiceObjectImpl executeLocaleI
    SEVERE: ServiceObjectI.executeLocaleI CONTRACT.WRITE

文件2:

Error code: CommonDomain.InvalidStateTransition
    at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1700)
    at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1558)
    at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:940)
    at com.sun.corba.se.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:198)
    at com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:712)

РЯа. 19, 2021 4:14:16 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:14:16 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:15:42 PM com.lhs.ccb.sfw.application.ServiceObjectImpl executeLocaleI
SEVERE: ServiceObjectI.executeLocaleI CONTRACT.WRITE

文件3:

Error code: CommonDomain.InvalidStateTransition
    at com.lhs.ccb.sfw.application.ServiceObjectImpl.execute(Unknown Source)
    at com.lhs.cil.core.AggregatedServiceObjectImpl.execute(Unknown Source)
    at com.lhs.ccb.sfw.application.ServiceObjectImpl.executeLocaleI(Unknown Source)
    at com.lhs.cil.core.AggregatedServiceObjectImpl.executeLocaleI(Unknown Source)
    at com.lhs.ccb.soi.ServiceObjectIPOA._invoke(Unknown Source)

РЯа. 19, 2021 4:20:21 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:20:21 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:21:22 PM com.lhs.ccb.sfw.application.ServiceObjectImpl executeLocaleI
SEVERE: ServiceObjectI.executeLocaleI CONTRACT.WRITE

Error code: CommonDomain.InvalidStateTransition
    at com.lhs.CommonDomain.bscs_core.ae.a(Unknown Source)
    at com.lhs.cil.core.AggregatedServiceObjectImpl.execute(Unknown Source)
    at com.lhs.ccb.sfw.application.ServiceObjectImpl.executeLocaleI(Unknown Source)
    at com.lhs.cil.core.AggregatedServiceObjectImpl.executeLocaleI(Unknown Source)
    at com.lhs.ccb.soi.ServiceObjectIPOA._invoke(Unknown Source)

РЯа. 19, 2021 4:21:22 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:21:22 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:26:22 PM com.lhs.ccb.sfw.application.ServiceObjectImpl executeLocaleI
SEVERE: ServiceObjectI.executeLocaleI CONTRACT.WRITE

Error code: CommonDomain.InvalidStateTransition
    at com.lhs.ccb.sfw.application.ServiceObjectImpl.execute(Unknown Source)
    at com.lhs.cil.core.AggregatedServiceObjectImpl.execute(Unknown Source)
    at com.lhs.ccb.sfw.application.ServiceObjectImpl.executeLocaleI(Unknown Source)
    at com.lhs.cil.core.AggregatedServiceObjectImpl.executeLocaleI(Unknown Source)
    at com.lhs.ccb.soi.ServiceObjectIPOA._invoke(Unknown Source)

РЯа. 19, 2021 4:26:22 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:26:22 PM com.lhs.FrameworkExtension.bscs_core.d getErrorMessage
WARNING: no localized text available for:
ErrorCode : InvalidStateTransition
BundleName: CommonDomain_ErrorDictionary
Locale    : en_US
Reason    : XMlResourceException: ResourceBundleManager: Can not find resource bundle 'CommonDomain_ErrorDictionary'
РЯа. 19, 2021 4:30:40 PM com.lhs.ccb.sfw.application.ServiceObjectImpl executeLocaleI
SEVERE: ServiceObjectI.executeLocaleI CONTRACT.WRITE

我想创建一个 shell 脚本,该脚本将以读取第一个文件的方式读取这些文件,并提取出 strict1 中的 SEVERE 行,然后提取 warning1 中的 WARNING 行。然后传递第二个文件,以便severe2中的SEVERE行,然后warning2中的WARNING行与最后一个文件(10个文件)相同。做到这一点最简单的方法是什么?

我检查的方法如下:**

for i in (ls *.log); do
cat *.log|grep "SEVERE" > severe$i.txt
cat *.log|grep "WARNING" > warning$i.txt
done;

**

答案1

您的代码存在几个问题:


for i in (ls *.log); do
  • 相反(...),它应该是$(...),但是,根本不需要,因为......
  • 你不应该解析的输出ls,而是使用for f in *.log; do
  • 你的$i不是计数器,而是文件名,你必须手动添加计数器。i=0在循环和增量之前,例如在i=$((i+1))循环内部。 (然后使用for f in *.log)。

cat *.log|grep "SEVERE" > severe$i.txt

虽然这本身并没有“错误”,但本案仍然存在问题。

  • grep从文件读取,不需要cat

    grep -h "SEVERE" *.log > severe$i.txt
    
  • *.log当您想要分别读取每个文件中的消息时,为什么还要读取呢?

  • 要提高性能,请将-F选项添加到您的grep.

  • 为了不匹配错误的模式而只匹配整个单词,请添加 -w.

  • 好习惯是双引号变量文件名 -->... > "severe$i.txt"


所以,总的来说,我会这样做:

i=0
for f in *.log; do
    i=$((i+1))
    grep -Fw "SEVERE" "$f" > "severe$i.txt"
    grep -Fw "WARNING" "$f" > "warning$i.txt"
done

或全部集中在一个awk

awk '
    FNR==1{FILENUM+=1}
    /^SEVERE/{print > "severe"FILENUM".txt"}
    /^WARNING/{print > "warning"FILENUM".txt"}
' *.log

相关内容