Text file splitting in multiple files by content in Powershell

前端 未结 1 1426
暖寄归人
暖寄归人 2020-12-21 15:31

i\'m reading a lot of threads - but nothing for me personal. I need to split text file in the following form:

---------------------  Instance Type and Transm         


        
相关标签:
1条回答
  • 2020-12-21 16:09

    Something like this should work:

    $InputFile = "c:\path\myfiletosplit.txt"
    $Reader = New-Object System.IO.StreamReader($InputFile)
    $a = 1
    While (($Line = $Reader.ReadLine()) -ne $null) {
        If ($Line -match "---------------------  Instance Type and Transmission --------------") {
            $OutputFile = "MySplittedFileNumber$a.txt"
            $a++
        }    
        Add-Content $OutputFile $Line
    }
    

    or whitout .net class:

    $a = 1
    gc "c:\path\myfiletosplit.txt" | % {    
        If ($_ -match "---------------------  Instance Type and Transmission --------------") {
            $OutputFile = "MySplittedFileNumber$a.txt"
            $a++
        }    
        Add-Content $OutputFile $_
    }
    
    0 讨论(0)
提交回复
热议问题