Windows命令行之powershell & cmd的使用--持续更

1. 使用 powershell 的 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的实现。
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

 

注意:
powershell里的编码是 GBK的,不是 UTF8,如果要过滤中文字符,简单的方法是先将 UTF8 编码的文件转换为 ANSI 编码。

参考:https://www.cnblogs.com/myfjd/p/4076256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值