如何在PowerShell中阻止此无限循环?

时间:2013-08-23 14:36:01

标签: bash powershell

假设我有几个包含单词'not'的文本文件,我想找到它们并创建一个包含匹配项的文件。在Linux中,我可能会这样做

grep -r not *.txt > found_nots.txt

这很好用。在PowerShell中,以下回应我想要的屏幕

get-childitem *.txt -recurse | select-string not

但是,如果我将其传输到文件:

get-childitem *.txt -recurse | select-string not > found_nots.txt

它运行了很长时间。我最终CTRL-C退出并查看了find_nots.txt文件,这个文件非常庞大。看起来PowerShell包含输出文件作为要搜索的文件之一。每次添加更多内容时,都会发现要添加的内容。

如何阻止此行为并使其行为更像Unix版本?

2 个答案:

答案 0 :(得分:4)

使用-Exclude选项。

get-childitem *.txt -Exclude 'found_nots.txt' -recurse | select-string not > found_nots.txt

答案 1 :(得分:3)

第一个简单的解决方案是将文件输出扩展名重命名为另一个

相关问题