包括一個PowerShell解決方案的完整性。
$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
這將讀取輸入文件,並生成一個自定義對象數組,將文本與其行號關聯起來。該腳本使用Get-Random
來選擇一半線條。 Get-Random不保留順序,因此腳本會根據原始行號進行排序。然後按順序提取原始行並寫出它們。
以上腳本需要PSCustomObject的PowerShell 3.0。 3.0版預裝在Windows 7及更高版本上。如果您需要在Vista上運行,則可以使用PSObject代替,如this answer或this blog post中所示。
請注意,如果行順序無關緊要,則此腳本變得更簡單。
$lines = Get-Content input.txt
$lines | Get-Random -count ($lines.length/2) | Set-Content output.txt
要從批處理文件或CMD提示符運行PowerShell腳本,請使用以下命令。
powershell.exe -ex bypass -file yourscript.ps1
或者,如果你的腳本在一行完全適合,沒有必要創建一個單獨的PS1
powershell.exe -c "$l = gc input.txt; $l | Get-Random -c ($l.length/2) | sc output.txt"
爲什麼這需要成爲批處理文件? –
因爲我只能批量編程。 –
發佈您嘗試過的內容,您的代碼和行不通的行。 SO不是一個寫我的軟件網站。 – 2015-09-05 20:11:26