파일 읽기 행별로 PowerShell) 에서

내가 원하는 파일을 읽을 수 있는 행별로 PowerShell. 특히, I want to 루프 (loop) 를 통해 파일 저장 및 처리, 각 행에 대한 몇 가지 변수를 사용한다.

내가 알고 있는 Bash avamer:

while read line do
    if [[ $line =~ $regex ]]; then
          # work here
    fi
done < file.txt

별로 관한 문서가 PowerShell 반복됩니다.

질문에 대한 의견 (4)
해결책

&gt. 별로 관한 문서가 PowerShell 반복됩니다.

에 관한 문서가 루프를 PowerShell 은 할 수 있습니다 다음 도움말에서는 체크아웃합니다 많기 및 주제: ['about_For'] (https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_for), ['about_ForEach'] (https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_foreach), ['about_Do'] (https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_do), ['about_While'] (https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_while).

foreach($line in Get-Content .\file.txt) {
    if($line -match $regex){
        # Work here
    }
}

또 다른 문제에 대한 해결책을 파이프 라인을 성어는 PowerShell) 는 텍스트 파일을 ['포리치 객체에는' cmdlet] (https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/foreach-object):

Get-Content .\file.txt | ForEach-Object {
    if($_ -match $regex){
        # Work here
    }
}

대신, 파이프 라인을 통해 정규 표현식 매칭 루프 내에 수 있습니다 (https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Core/Where-Object) ['여기서 객체에는'] # 39, 바로 이런 관심이 필터링하려면 you& re:

Get-Content .\file.txt | Where-Object {$_ -match $regex} | ForEach-Object {
    # Work here
}
해설 (3)

'나쁜 컨텐츠 확보하십시오.' 의 성능. 이 한 번에요 메모리에 파일을 읽을 수 있는 것이다.

C # (.NET) 파일 판독기에서 각 라인을 하나씩 판독합니다

  • Best 페퍼메이스 *
foreach($line in [System.IO.File]::ReadLines("C:\path\to\file.txt"))
{
       $line
}

강력한 또는 약간 덜

[System.IO.File]::ReadLines("C:\path\to\file.txt") | ForEach-Object {
       $_
}

이 '포리치' 보다 '포리치 객체에는 약간 기술서임을 될 가능성이 높다' (자세한 내용은 아래 설명 참조).

해설 (12)

그분의 스위치였습니다 제대로 있습니다.

"' & # 39 개 2

39 three&. &gt. 파일

$ # 39, & # 39, t& regex = ^

regex 스위치 - - 파일을 { regex 는 {&quot _&quot, 선, $ $ } } "'

출력:

"' 보통 2 호선 3 호선 "'

해설 (0)