Batch file to delete half the lines of a text file at random?

前端 未结 6 391
囚心锁ツ
囚心锁ツ 2021-01-29 11:27

I need a way to use batch to look at every line of a text file, and delete half of the lines in that text file, choosing which lines to delete at random.

This is to simu

6条回答
  •  不知归路
    2021-01-29 12:22

    Including a PowerShell solution for completeness.

    $lines = Get-Content input.txt
    $lines | foreach { $i=0 } {
        [PSCustomObject]@{index=$i;line=$_}
        $i++
    } |
    Get-Random -count ($lines.length / 2) |
    sort index |
    select -Expand line |
    Set-Content output.txt
    

    This reads the input file, and builds an array of custom objects associating the text with its line number. The script uses Get-Random to select half the lines. Get-Random does not preserve order, so the script then sorts on the original line number. It then extracts the original lines in order and writes them out.

    This script above requires PowerShell 3.0 for the PSCustomObject. Version 3.0 comes preinstalled on Windows 7 and above. If you need to run on Vista, you can use PSObject instead, as shown in this answer or this blog post.

    Note that if the line order doesn't matter, then this script becomes much simpler.

    $lines = Get-Content input.txt
    $lines | Get-Random -count ($lines.length / 2) | Set-Content output.txt
    

    To run a PowerShell script from a batch file or the CMD prompt, use the following.

    powershell.exe -ex bypass -file yourscript.ps1
    

    Or if your script fits entirely in one line, no need to create a separate ps1

    powershell.exe -c "$l = gc input.txt; $l | Get-Random -c ($l.length / 2) | sc output.txt"
    

提交回复
热议问题