Solved

how to blend these txt lines in a random way?

Posted on 2016-10-18
8
44 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 12

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 52

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
 
LVL 12

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
Want to promote your upcoming event?

Attending an event? Speaking at a conference? Or exhibiting at a trade show? Easily inform your contacts by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

 
LVL 52

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 12

Accepted Solution

by:
Dustin Saunders earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now