Get a subset of lines from a big text file using PowerShell v2

后端 未结 5 808
终归单人心
终归单人心 2020-12-30 11:57

I\'m working with a big text file, I mean more than 100 MB big, and I need to loop through a specific number of lines, a kind of subset so I\'m trying with this,

<
5条回答
  •  半阙折子戏
    2020-12-30 12:54

    Are the rows of fixed length? If they are, you can seek to desired position by simply calculating offset*row length and using something like .Net FileStream.Seek(). If they are not, all you can do is to read file row by row.

    To extract lines m, n, try something like

    # Open text file
    $reader = [IO.File]::OpenText($myFile)
    $i=0
    # Read lines until there are no lines left. Count the lines too
    while( ($l = $reader.ReadLine()) -ne $null) {
        # If current line is within extract range, print it
        if($i -ge $m -and $i -le $n) {
            $("Row {0}: {1}" -f $i, $l)
        }
        $i++
        if($i -gt $n) { break } # Stop processing the file when row $n is reached.
    }
    # Close the text file reader
    $reader.Close()
    $reader.Dispose()
    

提交回复
热议问题