我想从文本文件中查找包含关键字的单词。示例文本文件(test.txt)包含以下字符串。
Here is unmatched part.
" Happynimall happy-monkey:2 happy-cat:5 "
Here is unmatched part too. Here is unmatched part too.
所以我尝试了如下的powershell命令。
$(gc test.txt).replace(' ',"`n")|select-string 'monkey','cat'
但结果未能像这样只提取匹配的字线。
"
Happynimall
happy-monkey:2
happy-cat:5
"
顺便说一句,cmd“findstr”在提取管道流中生成的匹配字行时效果很好。
powershell -command "$(gc test.txt).replace(' ',"""`n""")"|findstr "monkey cat"
happy-monkey:2
happy-cat:5
为什么“select-string”不能只提取匹配的字行?为什么“findstr”可以做到这一点?如何使 select-string 与 findstr 给出相同的结果?有没有人可以帮助我理解它并使 select-string 做到这一点?提前致谢:-)