?
Solved

how to blend these txt lines in a random way?

Posted on 2016-10-18
8
Medium Priority
?
63 Views
Last Modified: 2016-10-18
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
0
Comment
Question by:john lambert
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:Dustin Saunders
ID: 41848765
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

1
 
LVL 59

Expert Comment

by:Bill Prew
ID: 41848786
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
0
 

Author Comment

by:john lambert
ID: 41848819
for WINDOWS OS please..and for BIGFILE.TXT
thank you
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:Dustin Saunders
ID: 41848837
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'
1
 
LVL 59

Expert Comment

by:Bill Prew
ID: 41848846
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
2
 

Author Comment

by:john lambert
ID: 41848853
working perfect but can u add please OUTPUT.TXT ??
0
 
LVL 14

Accepted Solution

by:
Dustin Saunders earned 2000 total points
ID: 41848854
Just change the save path on Set-Content and you're good to go.
$list = Get-Content "C:\test\list.txt"
$list = $list | Sort-Object {Get-Random}
Set-Content "C:\test\OUTPUT.txt" $list

Open in new window

1
 

Author Closing Comment

by:john lambert
ID: 41848861
great, thank you...
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a real story and is one of my scariest tech experiences. Most users think that IT experts like us know how to fix all computer problems. However, if there is a time constraint and you MUST not fail the task or you will lose your job, a simple …
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question