使用 powershell 的 grep 過濾文本
有個log文件,大小在4M左右,要求找出里面耗時超過100s 的記錄。首先想到了強大的 grep ,那么就搞起。
先在網上找一下資料,這篇文章,有幾種方式:
第一種:
Get-content somefile.txt|findstr "someregexp"
Get-content可以換成cat,Powershell已經給他們做了個別名,可真是體諒sheller。
這種方法算是commandline和Powershell混合,因為findstr是命令行工具,並不是Powershell的cmdlet。
第二種:
cat somefile.txt | where { $ -match "some_regexp"}
純種Powershell實現了,利用了where過濾
第三種:
Select-String "some_regexp" somefile.txt
直接用Select-string的實現。
經過測試,最后寫出的 powershell 命令如下:
cat .\log.log|where {$_ -match "\d{3,}\.\d{2,}s"} >>result.log
用了 where 這個, 這個能使用正則, findstr 命令不行。里面的正則匹配字符串 "\d{3,}.\d{2,}s" 也很簡單了,"3個數字.2個數字以上s "的意思。
最后: 過濾出來的結果放入 result.log
17:05:14,884 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor7 DEBUG StrategyActionHelper: - getStrategyInvoiceMap finished ... Consumed time:191.028s
17:05:14,889 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor4 DEBUG StrategyActionHelper: - getStrategyInvoiceMap finished ... Consumed time:191.04s
17:07:19,112 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor7 DEBUG StrategyActionHelper: - setListStrategyAttributes finished ... Consumed time:379.082s
17:07:20,106 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor4 DEBUG StrategyActionHelper: - setListStrategyAttributes finished ... Consumed time:381.021s
17:07:37,449 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor4 DEBUG StrategySearchAction: - setListStrategyAttributes finished ... Consumed time:398.364s
17:25:26,773 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor4 DEBUG cl: - build table data in getClientContractElement finished ... Consumed time:1064.296s
17:25:27,328 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor4 DEBUG cl: - getClientContractElement finished ... Consumed time:1064.858s
17:25:27,328 ltcappserver.node1@ltcappserver http-0.0.0.0-8888-Processor4 DEBUG cl: - buildGTReport finished ... Consumed time:1064.87s Free memory: 176198