9

私は大きなテキスト ファイルを扱っています。つまり、100 MB 以上の大きさで、特定の行数、一種のサブセットをループする必要があるので、これを試しています。

$info = Get-Content -Path $TextFile | Select-Object -Index $from,$to
foreach ($line in $info)
{
,,,

しかし、うまくいきません。サブセットの最初の行だけを取得するようなものです。

Index 属性に関するドキュメントが見つからないので、これは可能ですか、それともファイル サイズを考慮して別のアプローチを使用する必要がありますか?

4

5 に答える 5

14
PS> help select -param index

-Index <Int32[]>
    Selects objects from an array based on their index values. Enter the indexes in a comma-separated list.

    Indexes in an array begin with 0, where 0 represents the first value and (n-1) represents the last value.

    Required?                    false
    Position?                    named
    Default value                None
    Accept pipeline input?       false
    Accept wildcard characters?  false

上記に基づいて、「8,13」は 2 行だけになります。できることの 1 つは、数値の配列を渡すことです。範囲演算子を使用できます。

Get-Content -Path $TextFile | Select-Object -Index (8..13) | Foreach-Object {...}
于 2013-01-16T07:52:34.690 に答える
4

行は固定長ですか?offset*row lengthそうである場合は、 .Netのようなものを計算して使用するだけで、目的の位置を探すことができますFileStream.Seek()。そうでない場合は、ファイルを1行ずつ読み取るだけです。

行m、nを抽出するには、次のようなものを試してください。

# 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()
于 2013-01-16T07:27:42.420 に答える
0

Get-Content コマンドレットには、readcount パラメーターと totalcount パラメーターがあります。私はそれらをいじって、興味のある行がオブジェクトに割り当てられるように設定してから、そのオブジェクトをループに使用します。

于 2014-11-20T21:39:06.340 に答える