Solved

how to blend these txt lines in a random way?

Posted on 2016-10-18
8
38 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 51

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
Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

 
LVL 51

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

758 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

21 Experts available now in Live!

Get 1:1 Help Now