【问题标题】:How to randomly rearrange lines in a text file using a batch file如何使用批处理文件随机重新排列文本文件中的行
【发布时间】:2013-10-23 23:04:58
【问题描述】:

我正在创建一个随机删除不同 MAC 地址的代码,但不知道如何执行此操作。我对如何解决此问题的想法是使用此脚本随机化或重新排列文本文件中 MAC 地址的顺序,但我无法弄清楚如何使用批处理文件执行此操作。它的工作原理是它将读取“maclist.txt”,然后使用随机顺序“maclist_temp.txt”创建一个新的临时文件,这将是重新排列的文件。然后,它会按顺序拉取这个随机文件。

我尝试过 Google 和搜索网络,但没有发现任何有用的东西。我仍在积极寻找,但任何建议都会非常有用。

提取和删除随机行然后添加到底部这样简单的操作可能会起作用。随机化会更好,但我想保留原始列表。比如:

  1. 制作名为 maclist_temp.txt 的 maclist.txt 的临时副本
  2. 随机取一个 MAC 地址,从 maclist_temp.txt 中删除
  3. 读到底

这就是我想要的,但欢迎任何建议。

【问题讨论】:

标签: file batch-file text random


【解决方案1】:

你可以试试这个batch file 来帮助你洗牌你的maclist.txt。批号的用法是

C:\> type list.txt | shuffle.bat > maclist_temp.txt

这里是shuffle.bat的内容:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET TmpFile=tmp%RANDOM%%RANDOM%.tmp
TYPE NUL >%Tmpfile%
FOR /F "tokens=*" %%i IN ('MORE') DO SET Key=!RANDOM!!RANDOM!!RANDOM!000000000000& ECHO !Key:~0,15!%%i>> %TmpFile%
FOR /F "tokens=*" %%i IN ('TYPE %TmpFile% ^| SORT') DO SET Line=%%i&ECHO.!Line:~15!
::DEL %TmpFile%
ENDLOCAL

发出上述命令后,maclist_temp.txt 将包含一个随机的 MAC 地址列表。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这似乎有效。将文件的命令行参数提供给它以进行随机化。

        @echo off
        setlocal EnableDelayedExpansion
    
        rem read the number of lines in the file
        rem the find prepends the line number so we catch blank lines
        for /f "delims=" %%n in ('find /c /v "" %1') do set "len=%%n"
        set len=!len:*: =!
        rem echo %1 has %len% lines
    
        rem Relocate as many lines as there are lines in the file
        for /l %%j in (1 1 !len!) do (
          rem echo starting round %%j
    
          rem geta random number between 1 and the number of lines in the file
          set /a var=!random! %% !len! + 1
          rem echo relocating line !var!
    
          rem make sure there is no temp file
          if exist %1.temp del %1.temp
    
          rem read each line of the file, write any that don't match and then write the one that does
          <%1 (
            for /l %%i in (1 1 !len!) do (
    
              rem if it is the target line then save it
              if %%i == !var! (
                set /p found=
                rem echo saving !found!
              )
    
              rem if it is the target line then write it
              if not %%i == !var! (
                set /p other=
                rem echo writing !other!
                echo !other!>> %1.temp
              )
            )
            rem now write the target line at the end
            rem echo appending !found!
            echo !found!>> %1.temp
          )
    
          rem replace the original with the temp version
          move %1.temp %1>nul
        )
    
        rem print the result
        type %1
    

    【讨论】:

    • 我的比 shuffle.bat 复杂得多——为什么我没有想到这样做呢???
    【解决方案3】:

    这是一种更简单的随机化/随机化文件的方法,不需要临时文件。您甚至可以重复使用相同的输入文件名。

    限制是:空白行和以; 开头的行将被跳过,以= 开头的行将去掉所有前导= 符号,^ 字符加倍。

    @echo off
    setlocal
    for /f "delims=" %%a in (maclist.txt) do call set "$$%%random%%=%%a"
    (for /f "tokens=1,* delims==" %%a in ('set $$') do echo(%%b)>newmaclist.txt
    endlocal
    

    【讨论】:

    • 还有一个限制:字面上的% 符号会丢失...不过,这是一种很好的方法!也许您对my answer 感兴趣,它也解决了潜在重复random 数字的问题...
    【解决方案4】:

    放入cmd文件

    for /f "tokens=2 delims=/" %%m in ('cmd /e:on /v:on /c "for /f %%f in (maclist.txt) do @echo !random!/%%f" ^| sort') do echo %%m
    

    它生成一个 cmd,它读取内部的 mac 列表,为 mac 加上一个随机值和斜线前缀,并对列表进行排序。然后将该列表在外部拆分,以使用斜杠作为分隔符并打印 mac 地址。

    【讨论】:

      【解决方案5】:

      我真的很喜欢foxidriveapproach。尽管如此,我想提供一个解决方案,消除所有列出的限制(尽管与cmd 相关的限制,如文件大小

      键是delayed expansion,需要切换才能不丢失感叹号。这解决了像^&amp;%!()&lt;&gt;| 和 @987654 等特殊字符的所有潜在问题。

      计数器index 已实现,以免丢失原始文本文件的一行,否则可能会发生这种情况,因为random 可能返回重复值;附加了index,生成的变量名称$$!random!.!index! 是唯一的。

      findstr /N /R "^" 命令在原始文件的每一行之前加上一个行号,后跟一个冒号。因此,for /F 循环不会出现任何空行,它会忽略此类。行号也隐含地解决了前导分号的问题,for /F 的默认 eol 字符。

      最后,直到并包括第一个冒号的所有内容(请记住由findstr 添加的上述前缀)在输出之前从每一行中删除,因此不再有前导等号被忽略。

      代码如下:

      @echo off
      setlocal EnableExtensions DisableDelayedExpansion
      
      set /A "index=0"
      for /f "delims=" %%a in ('findstr /N /R "^" "%~dpn0.lst"') do (
          setlocal EnableDelayedExpansion
          for /F %%b in ("$$!random!.!index!") do (
              endlocal
              set "%%b=%%a"
          )
          set /A "index+=1"
      )
      > "%~dpn0.new" (
          for /f "delims=" %%a in ('set $$') do (
              set "item=%%a"
              setlocal EnableDelayedExpansion
              echo(!item:*:=!
              endlocal
          )
      )
      
      endlocal
      exit /B
      

      【讨论】:

        猜你喜欢
        • 2012-10-31
        • 1970-01-01
        • 2016-05-15
        • 1970-01-01
        • 2011-11-15
        • 2022-10-18
        • 2013-11-21
        • 2018-12-31
        • 1970-01-01
        相关资源
        最近更新 更多