繁体   English   中英

在powershell中使用select-string排除搜索模式

[英]exclude search pattern with select-string in powershell

我正在使用select字符串来搜索文件中的错误。 是否可以像使用grep一样排除搜索模式。 例如:

grep ERR* | grep -v "ERR-10"

select-string -path logerror.txt -pattern "ERR"

logerror.txt

OK
ERR-10
OK
OK
ERR-20
OK
OK
ERR-10
ERR-00

我想得到所有的ERR线,但不是ERR-00和ERR-10

我为此使用“-NotMatch”参数

PS C:\>Get-Content .\some.txt
1
2
3
4
5
PS C:\>Get-Content .\some.txt | Select-String -Pattern "3" -NotMatch    
1
2
4
5

对于您的情况,答案是:

Get-Content .\logerror.txt | Select-String -Pattern "ERR*" | Select-String -Pattern "ERR-[01]0" -NotMatch

我想你可以Where-Object这里使用Where-Object

Write-Output @"
OK
ERR-10
OK
OK
ERR-20
OK
OK
ERR-10
ERR-00
"@ > "C:\temp\log.txt"

# Option 1.
Get-Content "C:\temp\log.txt" | Where-Object { $_ -Match "ERR*"} | Where-Object { $_ -NotMatch "ERR-[01]0"}

# Option 2.
Get-Content "C:\temp\log.txt" | Where-Object { $_ -Match "ERR*" -and $_ -NotMatch "ERR-[01]0"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM