텍스트 파일에 문자열이 포함된 경우 PowerShell을 사용하여 텍스트 파일에서 줄 제거
아래 PowerShell 코드를 사용하여 부분 문자열이 포함된 텍스트 파일에서 모든 줄을 제거하려고 합니다.
Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt
실제 문자열은H|159|28-05-2005|508|xxx
파일에서 여러 번 반복되며 위에서 지정한 첫 번째 부분만 일치시키려고 합니다.그것이 맞습니까?현재 출력이 비어 있습니다.
내가 뭘 빼놓았나요?
동일한 파일에 기록하려면 다음과 같이 하면 됩니다.
Set-Content -Path "C:\temp\Newtext.txt" -Value (get-content -Path "c:\Temp\Newtext.txt" | Select-String -Pattern 'H\|159' -NotMatch)
백티크를 사용하여 | 문자 이스케이프
get-content c:\new\temp_*.txt | select-string -pattern 'H`|159' -notmatch | Out-File c:\new\newfile.txt
기존 답변을 기반으로 동일한 파일에 쓰기 위한 또 다른 옵션입니다.내용이 파일로 전송되기 전에 수행을 완료하려면 괄호를 추가하기만 하면 됩니다.
(get-content c:\new\sameFile.txt | select-string -pattern 'H`|159' -notmatch) | Set-Content c:\new\sameFile.txt
필요없습니다Select-String
이 경우, 그냥 라인을 걸러냅니다.
Get-Content C:\new\temp_*.txt |
Where-Object { -not $_.Contains('H|159') } |
Set-Content C:\new\newfile.txt
String.Contains
정규식 대신 문자열 비교를 수행하므로 파이프 문자에서 벗어날 필요가 없으며 더 빠릅니다.
파이프 문자|
정규식에 특별한 의미가 있습니다.a|b
일치 또는 일치를 의미합니다.a
또는b
리터럴을 일치시키려면|
캐릭터, 당신은 탈출해야 합니다:
... | Select-String -Pattern 'H\|159' -NotMatch | ...
이 방법은 단순한 문제를 해결하는 데 오랜 시간이 걸릴 수 있으며, 일치하는 항목이 여러 개 포함된 줄을 제거할 수 있습니다.저는 사용할 수 있는 부분적인 일치가 없었고, 1000개 이상의 파일에 대해 수행해야 했습니다.이 게시물은 제가 필요한 곳으로 가는 데 도움이 되었습니다, 감사합니다.
$ParentPath = "C:\temp\test"
$Files = Get-ChildItem -Path $ParentPath -Recurse -Include *.txt
$Match1 = "matchtext1"
$Match2 = "matchtext2"
$Match3 = "matchtext3"
$Match4 = "matchtext4"
$Match5 = "matchtext5"
$Match6 = "matchtext6"
$Match7 = "matchtext7"
$Match8 = "matchtext8"
$Match9 = "matchtext9"
$Match10 = "matchtext10"
foreach ($File in $Files) {
$FullPath = $File | % { $_.FullName }
$OldContent = Get-Content $FullPath
$NewContent = $OldContent `
| Where-Object {$_ -notmatch $Match1} `
| Where-Object {$_ -notmatch $Match2} `
| Where-Object {$_ -notmatch $Match3} `
| Where-Object {$_ -notmatch $Match4} `
| Where-Object {$_ -notmatch $Match5} `
| Where-Object {$_ -notmatch $Match6} `
| Where-Object {$_ -notmatch $Match7} `
| Where-Object {$_ -notmatch $Match8} `
| Where-Object {$_ -notmatch $Match9} `
| Where-Object {$_ -notmatch $Match10}
Set-Content -Path $FullPath -Value $NewContent
Write-Output $File
}
제안한 작업을 수행하는 동안 이 문제가 발생한 사람이 있다면,Robert Brooker
-
*These files have different encodings. Left file: Unicode (UTF-8) with signature. Right file: Unicode (UTF-8) without signature. You can resolve the difference by saving the right file with the encoding Unicode (UTF-8) with signature.*
와 함께Set-Content
사용하다-Encoding UTF8
그래서 이렇게
(get-content c:\new\sameFile.txt | select-string -pattern 'H`|159' -notmatch) | Set-Content c:\new\sameFile.txt -Encoding UTF8
언급URL : https://stackoverflow.com/questions/24326207/using-powershell-to-remove-lines-from-a-text-file-if-it-contains-a-string
'programing' 카테고리의 다른 글
MariaDB 트리거에서 JSON_EXTRACT 사용 (0) | 2023.08.05 |
---|---|
href 속성이 없는 링크에서 작은 손을 어떻게 보여줄 수 있습니까? (0) | 2023.08.05 |
Node.js HTTP 서버를 사용하여 단일 쿠키 가져오기 및 설정 (0) | 2023.08.05 |
배경 이미지와 불투명도를 동일한 속성으로 설정할 수 있습니까? (0) | 2023.07.31 |
Enum에서 문자열을 검색하고 Enum을 반환합니다. (0) | 2023.07.31 |