加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Findstr – 仅返回正则表达式匹配

发布时间:2020-12-14 06:00:48 所属栏目:百科 来源:网络整理
导读:我在文本文件(test.txt)中有这个字符串: BLA BLA BLABLA BLAFound 11 errors and 7 warnings 我执行此命令: findstr /r "[0-9]+ errors" test.txt 为了获得11个错误字符串. 相反,输出是: Found 11 errors and 7 warnings 有人可以帮忙吗? 解决方法 finds
我在文本文件(test.txt)中有这个字符串:

BLA BLA BLA
BLA BLA
Found 11 errors and 7 warnings

我执行此命令:

findstr /r "[0-9]+ errors" test.txt

为了获得11个错误字符串.

相反,输出是:

Found 11 errors and 7 warnings

有人可以帮忙吗?

解决方法

findstr工具不能仅用于提取匹配项.使用Powershell要容易得多.

这是一个例子:

$input_path = 'c:psin.txt'
$output_file = 'c:psout.txt'
$regex = '[0-9]+ errors'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

有关如何使用上述脚本的信息,请参阅the Windows PowerShell: Extracting Strings Using Regular Expressions article.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读