首页 > 解决方案 > 直接从 shell 搜索关键字并获取计数,而无需将日志保存到外部文件中

问题描述

我正在执行我的 jar 文件,它将在 shell 中创建日志。我的要求是直接从 shell 中搜索关键字,而不创建日志文件。使用此代码执行

`E:\Innoviti\BankInterfaceAumation>java -jar BankInterfaceAumation.jar 1`
Here is the logs 

这是我执行 jar 时创建的日志

` 3:000000:000000 PASSED4:null:000000001600       FAILED
39:00:00        PASSED------------------------Response Conditions Passed----------------------------
AFter Sale terminal is null
2018-09-06 13:23:23 INFO  UniPayTerminalDataFetcherImpl:33 - Fetching unipay terminal with id
Cached terminal or after sale terminal is null/could not be fetched cached terminal is nullAfter sale txn bank terminal  null
 base transaction  is null/could not be fetched
Executing unipay terminal assertions  ********************************************

Test Scenario total_txn_amt null SAME null Passed
Comapring property key_exchanged_on value expression SAME
Comparing Simple Exrepssion property SAME
Test Scenario key_exchanged_on null SAME null Passed
Comapring property utid_state value expression 00
Test Scenario utid_state null 00  Failed
Comapring property key_exchanged value expression SAME

L00005022018090613231871319216803136 失败`

关键字 =Failed

标签: shellunix

解决方案


您可以将日志“管道”到:

 grep -o 'Failed' | wc -l

这将计算“失败”的出现次数。如果您需要搜索不区分大小写的命令,则该grep命令变为grep -io

您的命令将变为:

E:\Innoviti\BankInterfaceAumation>java -jar BankInterfaceAumation.jar 1 | grep -o 'Failed' | wc -l

如果您想要一个执行示例,您可以尝试:

echo -e 'first line\nfailed Failed\nanother line with failed\nlast line' | grep -io 'failed' | wc -l

该命令的解释可以在这里找到:https ://explainshell.com/explain?cmd=echo+-e+%27Some+Failed+text%27+%7C+grep+-o+%27Failed%27+%7C+wc+- l


推荐阅读