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

前端 未结 6 392
囚心锁ツ
囚心锁ツ 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:21

    This is a native Windows batch script using Jscript to randomise the lines of a text file and print half of them.

    The size of the file is limited to the RAM that Jscript is able to request.

    @if (@X)==(@Y) @end /* harmless hybrid line that begins a JScript comment
    :batch file portion
    @echo off
    if "%~1"=="" (
    echo(Purpose: Randomises a text file - prints every 2nd random line
    echo(
    echo(Syntax: "%~0" "inputfile.txt" ^> "outputfile.txt"
    echo(
    pause
    goto :EOF
    )
    
    :: Randomises a file
    ::
    :: Reads the lines of file %1 into a jscript sparse array
    :: with a random number and space before each line.
    :: The array is sorted using the random numbers,
    :: and the randomised lines are printed - prints every 2nd random line
    
    
    @echo off
    cscript //E:JScript //nologo "%~f0" %* 
    :pause
    exit /b
    
    
    ************ JScript portion ***********/
    
      e=0;
      var array = new Array();
      fso = new ActiveXObject("Scripting.FileSystemObject");
      input=fso.OpenTextFile((WScript.Arguments(0)),1);
    
      while (!input.AtEndOfStream) {
        array[e]=Math.random()+" "+input.ReadLine();
        e++;
      }
      input.close();
    
      array.sort();
    
      var re = new RegExp(".*? (.*)");
    
      c=0;
      while (c

提交回复
热议问题