首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取多个文件

如何读取多个文件
EN

Unix & Linux用户
提问于 2021-05-06 12:19:34
回答 1查看 83关注 0票数 -2

FILE1:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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脚本,它将以读取第一个文件的方式读取这些文件,并在severe1中提取严重的行,然后提取warning1中的警告行。然后传递第二个文件,以便severe2中的严重行,然后warning2中的警告行与最后一个文件(10个文件)相同。做这件事最简单的方法是什么?

我检查的方式如下:**

代码语言:javascript
复制
for i in (ls *.log); do
cat *.log|grep "SEVERE" > severe$i.txt
cat *.log|grep "WARNING" > warning$i.txt
done;

**

EN

回答 1

Unix & Linux用户

发布于 2021-05-06 13:43:58

您的代码有几个问题:

代码语言:javascript
复制
for i in (ls *.log); do
  • 而不是(...),它应该是$(...),但是根本不需要它,因为.
  • 的输出不应该解析ls,而不是使用for f in *.log; do
  • 您的$i不是计数器,而是文件名,您必须手动添加计数器。循环和增量之前的i=0,例如循环中的i=$((i+1))。(然后使用for f in *.log)。
代码语言:javascript
复制
cat *.log|grep "SEVERE" > severe$i.txt

虽然这本身并不是“错误”的,但在这种情况下仍然存在问题。

  • grep从文件中读取,不需要cat:grep -h“严厉”*.log > severe$i.txt
  • 当您希望每个文件中的消息分开时,为什么要阅读*.log
  • 要提高性能,请将-F选项添加到grep中。
  • 若要不匹配错误的模式,而只匹配整个单词,请添加-w
  • 好习惯是将变量文件名双引号-> ... > "severe$i.txt"

所以,总的来说,我会:

代码语言:javascript
复制
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中:

代码语言:javascript
复制
awk '
    FNR==1{FILENUM+=1}
    /^SEVERE/{print > "severe"FILENUM".txt"}
    /^WARNING/{print > "warning"FILENUM".txt"}
' *.log
票数 3
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/648408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档