ユーザ用ツール

サイト用ツール


powershell:文章の分割
$inputFile = "C:\path\to\largefile.txt"
$outputDir = "C:\path\to\output\"
$linesPerFile = 10000
$counter = 0
$fileNumber = 1
$outputFile = Join-Path -Path $outputDir -ChildPath ("split_{0}.txt" -f $fileNumber)

# StreamReaderを使ってファイルを開く
$reader = [System.IO.File]::OpenText($inputFile)

try {
    while ($reader.Peek() -ge 0) { 
        $line = $reader.ReadLine()
        Add-Content -Path $outputFile -Value $line
        $counter++

        if ($counter -eq $linesPerFile) {
            $fileNumber++
            $outputFile = Join-Path -Path $outputDir -ChildPath ("split_{0}.txt" -f $fileNumber)
            $counter = 0
        }
    }
}
finally {
    $reader.Close()
}



$inputFile = "C:\path\to\largefile.txt"
$outputFile = "C:\path\to\outputfile.txt"

# 行を数えるカウンタ
$lineCounter = 0

# 出力する行を保持するための変数
$outputLine = $null

# StreamReaderを使ってファイルを開く
$reader = [System.IO.StreamReader]::new($inputFile)
$writer = [System.IO.StreamWriter]::new($outputFile)

try {
    while (-not $reader.EndOfStream) { 
        $line = $reader.ReadLine()
        $lineCounter++

        # 100,000行ごとの条件
        if ($lineCounter % 100000 -eq 0) {
            $writer.WriteLine($line)
        }
    }
}
finally {
    $reader.Close()
    $writer.Close()
}


ーーーー!
$inputFile = "C:\path\to\largefile.txt"

# 特定の文字列
$targetString = "YourTargetString"

# StreamReaderを使ってファイルを開く
$reader = [System.IO.StreamReader]::new($inputFile)

$skipLines = 0

try {
    while (-not $reader.EndOfStream) { 
        $line = $reader.ReadLine()

        # 文字列で始まる行が見つかった場合
        if ($line.StartsWith($targetString)) {
            $skipLines = 1000
        }

        # 行をスキップするかどうかを判断
        if ($skipLines -gt 0) {
            $skipLines--
            continue
        }

        # 行の処理や出力
        # この例では、行をコンソールに出力しています。
        # 必要に応じて、この部分を変更してください。
        Write-Output $line
    }
}
finally {
    $reader.Close()
}

ーーーーー
$inputFile = "C:\path\to\largefile.txt"
$lineCount = 10000 # 1ファイルあたりの行数
$counter = 0

Get-Content $inputFile | ForEach-Object {
    Add-Content -Path "C:\path\to\split_$(($counter/$lineCount).ToString('000')).txt" -Value $_
    $counter++
}
powershell/文章の分割.txt · 最終更新: 2023/09/01 11:30 by 119.231.18.70

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki