Link to home
Start Free TrialLog in
Avatar of john lambert
john lambert

asked on

how to blend these txt lines in a random way?

how to blend these txt lines in a random way?for example i have this:

john
john1
john123
miami
miami3
miami@1
london123
london

How to blend those lines to look, something like this:

john
miami
john1
london
john123
miami@1
london123
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Sort by random.

Example:

$list = "john","john1","john123","miami","miami3","miami@1"
$list = $list | Sort-Object {Get-Random}


foreach ($item in $list)
{
    Write-Host $item
}

Open in new window

Avatar of Bill Prew
Bill Prew

Nice @Dustin, and way better / shorter than anything hacked together in a DOS BAT approach.

I'm also not aware of any Windows non scripting tools (like SORT.EXE) that have any capability to randomize the output when "sorting".

~bp
Avatar of john lambert

ASKER

for WINDOWS OS please..and for BIGFILE.TXT
thank you
You can use Get-Content and the path to the file to get the list from text.

$list = Get-Content "C:\test\list.txt"
$list = $list | Sort-Object {Get-Random}


foreach ($item in $list)
{
    Write-Host $item
}

Open in new window


If you want to randomize and write the changes back to your text file, use:
$list = Get-Content "C:\test\list.txt"
$list = $list | Sort-Object {Get-Random}
Set-Content "C:\test\list.txt" $list

Open in new window

Or if you want to write to another file, just change the path after 'Set-Content'
And just to clarify, this is a WINDOWS OS solution using Powershell.  You can save the script shown above as a .ps1 file, and then run it from a command line or a batch file by doing:

powershell.exe -ExecutionPolicy Bypass -File EE28977195.ps1

Open in new window

~bp
working perfect but can u add please OUTPUT.TXT ??
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
great, thank you...