簡體   English   中英

如何使用批處理文件隨機重新排列文本文件中的行

[英]How to randomly rearrange lines in a text file using a batch file

我正在創建一個隨機剝離不同 MAC 地址的代碼,但無法弄清楚如何做到這一點。 我對如何解決這個問題的想法是使用此腳本隨機化或重新排列文本文件中 MAC 地址的順序,但我無法弄清楚如何使用批處理文件執行此操作。 這將如何工作,它將讀取“maclist.txt”,然后以隨機順序“maclist_temp.txt”創建一個新的臨時文件,這將是重新排列的文件。 然后,它將按順序拉出這個隨機文件。

我試過谷歌和搜索網絡,但我沒有發現任何有用的東西。 我仍在積極尋找,但任何建議都會非常有用。

像提取和刪除隨機行然后添加到底部這樣簡單的事情可能會奏效。 不過隨機化會更好,但我想保留原始列表。 類似的東西:

  1. 制作一個名為 maclist_temp.txt 的 maclist.txt 臨時副本
  2. 取一個隨機 MAC 地址,從 maclist_temp.txt 中刪除
  3. 讀到底部

這就是我想要的,但歡迎任何建議。

你可以試試這個批處理文件來幫助你洗牌你的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 地址列表。

希望這會有所幫助。

這是一種更簡單的隨機化/隨機化文件的方法,不需要臨時文件。 您甚至可以重復使用相同的輸入文件名。

限制是:空行和以;開頭的行將被跳過,以=開頭的行將刪除所有前導=符號,並且^字符加倍。

@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

這似乎有效。 向它提供文件的命令行參數以進行隨機化。

    @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

放入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,它讀取內部 for 中的 mac 列表,在 mac 前添加一個隨機值和一個斜杠,並對列表進行排序。 然后將此列表在外部拆分,以使用斜杠作為分隔符並打印 mac 地址。

我真的很喜歡Foxidrive方法 盡管如此,我想提供一個消除了所有列出的限制的解決方案(盡管與cmd相關的限制,如文件大小 < 2 GiB 和行長度 < ~ 8 KiB 仍然存在)。

關鍵是延遲擴展,需要切換才能不丟失解釋標記。 這解決了像^&%!等特殊字符的所有潛在問題! , ( , ) , < , > , | "

為了不丟失原始文本文件的一行,已經實施了計數器index ,否則可能會發生這種情況,因為random可能返回重復值; 附加index ,結果變量名$$!random!.!index! 是獨一無二的。

findstr /N /R "^"命令在原始文件的每一行之前都有一個行號,后跟一個冒號。 所以for /F循環來說,沒有任何一行看起來是空的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM