我正在使用select字符串来搜索文件中的错误.是否可以像使用grep一样排除搜索模式.例如:
grep ERR* | grep -v "ERR-10"
select-string -path logerror.txt -pattern "ERR"
Run Code Online (Sandbox Code Playgroud)
logerror.txt
OK
ERR-10
OK
OK
ERR-20
OK
OK
ERR-10
ERR-00
Run Code Online (Sandbox Code Playgroud)
我想得到所有的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
Run Code Online (Sandbox Code Playgroud)
对于您的情况,答案是:
Get-Content .\logerror.txt | Select-String -Pattern "ERR*" | Select-String -Pattern "ERR-[01]0" -NotMatch
Run Code Online (Sandbox Code Playgroud)
我想你可以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"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12028 次 |
| 最近记录: |