使用 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

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

使用 powershell 的 grep 过滤文本的更多相关文章

  1. [Linux] day07——查看及过滤文本

    查看及过滤文本 =====================================cat      concatenate         -n 添加行号------------------- ...

  2. [20170828]grep过滤技巧.txt

    [20170828]grep过滤技巧.txt --//经常使用grep过滤显示信息. #  ps -ef |grep oraagentoracle    13416      1  0  2016 ? ...

  3. grep 过滤.svn文件

    [grep 过滤.svn文件] 问题: 在repository搜索代码时,常常会搜索到.svn的代码,如果不想搜索.svn目录下的相关代码怎么办?    1.使用管道进行双层“过滤”,其中第二次gre ...

  4. 使用 awk 过滤文本或文件中的字符串

    当我们在 Unix/Linux 下使用特定的命令从字符串或文件中读取或编辑文本时,我们经常需要过滤输出以得到感兴趣的部分.这时正则表达式就派上用场了. 什么是正则表达式? 正则表达式可以定义为代表若干 ...

  5. shell入门-grep过滤-1

    正则表达式,就是一个字符串.有一定的规律.我们用指定的字符串匹配一个指定的行.指定的字符串就是正则表达式. 正则表达式有这几个工具:grep egrep sed awk 命令:gerep 说明:过滤出 ...

  6. PowerShell类grep

    PowerShell类grep 方法一: windows下没有grep不过有findstr, 功能差不多 方法二: powershell自带的正择功能 xxx | where {$_ -match & ...

  7. 整理grep实战文本搜索过滤技巧

    一:grep的简介: 文本搜索工具,根据用户指定的文本模式对目标文件进行逐行搜索,显示能够被模式所匹配到的行.配合正则表达式的使用可以实现强大的文本处理.下面一一说明正则的例子. 二:文本处理工具分类 ...

  8. Linux命令(八)过滤文本 grep

    grep 命令介绍 grep是一个强大的文本搜索工具命令,用于查找文件中符合指定格式的字符串,支持正则表达式.如不指定任何文件名称,或是文件名为 -,则gerp命令从标准输入设备中读取数据. grep ...

  9. grep搜索文本

    正则匹配: grep -E "[a-z]+" 只输出匹配到的文本: echo this is a line. | grep -o -E "[a-z]+\." 统 ...

随机推荐

  1. matlab实现共轭梯度法、多元牛顿法、broyden方法

    共轭梯度法: function [ x, r, k ] = CorGrant( x0, A, b ) x = x0; r = b - A * x0; d = r; X = ones(length(x) ...

  2. Unity Camera属性

    Camera属性 1.Clear Flags 清除标记:决定屏幕的那部分将被清除.当使用多个相机来描绘不同的游戏景象时,利用它是非常方便的. 2.Background 背景:在镜头中的所有元素描绘完成 ...

  3. Express/Koa/Hapi

    Express/Koa/Hapi 本文翻译自: https://www.airpair.com/node.js/posts/nodejs-framework-comparison-express-ko ...

  4. Aspose 导出excel小demo

    //转为pdf         private void CelltoPDF(string cellPath, string pdfPath)         {             Workbo ...

  5. vi 常用技巧

    vi/vim : Visual Interface/VI iMproved VI命令可以说是Unix/Linux世界里最常用的编辑文件的命令了,但是因为它的命令集众多,很多人都不习惯使用它,其实您只需 ...

  6. 【技术贴】解决MySql连接不上 ip远程连接Host is not allowed to conn

    落雨 如果你想连接远程IP的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL ...

  7. uva 10771

    思路题 K的人数只能以2减少 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...

  8. 解决asp.net mvc中*.resx资源文件访问报错

    个人笔记 问题重现 在asp.net mvc中,使用资源文件会出现一个问题,例如: 紧接着我进入视图界面,输入下面代码: <a href="javascript:void(0);&qu ...

  9. 140304笔记, mysql 更改自动增长字段值的重新设定

    1. 存在同名的或不同的namespace交错情况. Caused by: com.ibatis.sqlmap.client.SqlMapException: There is no result m ...

  10. 【redis】06Redis的高级应用之事务处理、持久化操作、pub_sub、虚拟内存

    上节课详细讲解了redis数据库的常用命令,以及redis数据库高级应用当中的, 安全性,跟咱们的主从复制, 这节课呢,咱们继续来讲咱们的高级应用, 首先来看一下咱们的事务处理, 事务处理 我前面说过 ...