programing

파워셸에 선택 문자열이 있는 검색 패턴 제외

kingscode 2023. 9. 18. 23:19
반응형

파워셸에 선택 문자열이 있는 검색 패턴 제외

선택 문자열을 사용하여 파일에 오류가 있는지 검색하고 있습니다.grep과 같이 검색 패턴을 제외할 수 있습니까?예:

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

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

log error.txt

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

ERR-00 및 ERR-10이 아닌 모든 ERR 라인을 받고 싶습니다.

이 경우 "-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여기서.

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"}

언급URL : https://stackoverflow.com/questions/39126832/exclude-search-pattern-with-select-string-in-powershell

반응형