我在文本文件(test.txt
)中有以下字符串:
BLA BLA BLA
BLA BLA
Found 11 errors and 7 warnings
我执行以下命令:
findstr /r "[0-9]+ errors" test.txt
为了得到只是11 errors
字符串。
相反,输出为:
Found 11 errors and 7 warnings
有人可以协助吗?
该FINDSTR工具不能用于仅提取匹配。为此,使用Powershell更加容易。
这是一个例子:
$input_path = 'c:\ps\in.txt'
$output_file = 'c:\ps\out.txt'
$regex = '[0-9]+ errors'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file
见的Windows PowerShell中:提取字符串使用正则表达式的文章如何使用上面的脚本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句