String 如何将正则表达式与powershell一起使用以删除特定行?

String 如何将正则表达式与powershell一起使用以删除特定行?,string,powershell,read-eval-print-loop,select-string,String,Powershell,Read Eval Print Loop,Select String,如何删除下面的匹配行: PS /home/nicholas/powershell/regex> PS /home/nicholas/powershell/regex> $doc = Get-Content ./foo.txt PS /home/nicholas/powershell/regex> PS /home/nicholas/powershell/regex> $doc a b c d e f g hmmm i

如何删除下面的匹配行:

PS /home/nicholas/powershell/regex> 
PS /home/nicholas/powershell/regex> $doc = Get-Content ./foo.txt
PS /home/nicholas/powershell/regex> 
PS /home/nicholas/powershell/regex> $doc                        


a
b
c
d
e
f
g
hmmm i j k l m
n
o
p
q


PS /home/nicholas/powershell/regex> 
PS /home/nicholas/powershell/regex> $doc | Select-String 'h'

hmmm i j k l m

PS /home/nicholas/powershell/regex> 
这只是一个示例,但需要遍历
$doc
以删除匹配的特定行

在这里,任何带有“h”的行都将被删除


从不同的角度看,返回所有其他不匹配的行。

感谢评论:

PS /home/nicholas/powershell/regex> 
PS /home/nicholas/powershell/regex> $doc | Select-String -notmatch 'h'



a
b
c
d
e
f
g
n
o
p
q



PS /home/nicholas/powershell/regex> 

似乎有效。

无需使用正则表达式进行相同的比较。如果
$doc
是一个数组,那么
$doc-ne'h i j k l m'
$doc是一个文本文件还是一个文本文件内容?尝试
$doc-split'\r?\n'|其中{!$.contains(“h”)}
选择字符串-不匹配“h”
$doc=@($doc)-不匹配“h”
也可以