Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Simple sort text file

Reply
 
Old 10-29-2008   #1 (permalink)
Jason


 
 

Simple sort text file

Trying to sort a text file that contains a list of names like "Firstname
Lastname"

Bob Smith
Andy Griffith
Jean-Luc Picard

I've got this to split the text into columns, but I feel that there's a
better way.

$a | select {$_.Split(" ")[0]}, {$_.Split(" ")[1]}

As a bonus question, how can I apply Column labels to those to columns I
just created?

My System SpecsSystem Spec
Old 10-29-2008   #2 (permalink)
Shay Levy [MVP]


 
 

Re: Simple sort text file



Check this: http://www.microsoft.com/technet/scr...pstip0425.mspx

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



J> Trying to sort a text file that contains a list of names like
J> "Firstname Lastname"
J>
J> Bob Smith
J> Andy Griffith
J> Jean-Luc Picard
J> I've got this to split the text into columns, but I feel that there's
J> a better way.
J>
J> $a | select {$_.Split(" ")[0]}, {$_.Split(" ")[1]}
J>
J> As a bonus question, how can I apply Column labels to those to
J> columns I just created?
J>


My System SpecsSystem Spec
Old 10-29-2008   #3 (permalink)
PaulChavez


 
 

RE: Simple sort text file

The select-object has a cool feature where you can give it a hashtable
instead of a property name to create a calculated property based on an
expression.

So assuming $a is the result of get-content on that text file, you can
construct a select expression like this and then pipe to sort-object and
set-content:

$a | select @{n="FirstName";e={$_.split(" ")[0]}},
@{n="LastNamee={$_.split(" ")[1]}} |
sort LastName |
set-content "Sorted Names.txt"


-Paul

"Jason" wrote:
Quote:

> Trying to sort a text file that contains a list of names like "Firstname
> Lastname"
>
> Bob Smith
> Andy Griffith
> Jean-Luc Picard
>
> I've got this to split the text into columns, but I feel that there's a
> better way.
>
> $a | select {$_.Split(" ")[0]}, {$_.Split(" ")[1]}
>
> As a bonus question, how can I apply Column labels to those to columns I
> just created?
My System SpecsSystem Spec
Old 10-30-2008   #4 (permalink)
Jason


 
 

Re: Simple sort text file

Beautiful! I was trying to use the "Expression" stuff, but the documentation
I was following was using "Label" instead of name, and it never did work for
me. This works great!

$a | select @{Name="Firstname";Expression={$_.Split(" ")[0]}},
@{Name="Lastname";Expression={$_.Split(" ")[1]}}

Thanks to both of you, Shay and Paul!

"Shay Levy [MVP]" wrote:
Quote:

>
>
> Check this: http://www.microsoft.com/technet/scr...pstip0425.mspx
>
> ---
> Shay Levy
> Windows PowerShell MVP
> http://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar: http://tinyurl.com/PSToolbar
>
>
>
> J> Trying to sort a text file that contains a list of names like
> J> "Firstname Lastname"
> J>
> J> Bob Smith
> J> Andy Griffith
> J> Jean-Luc Picard
> J> I've got this to split the text into columns, but I feel that there's
> J> a better way.
> J>
> J> $a | select {$_.Split(" ")[0]}, {$_.Split(" ")[1]}
> J>
> J> As a bonus question, how can I apply Column labels to those to
> J> columns I just created?
> J>
>
>
>
My System SpecsSystem Spec
Old 10-30-2008   #5 (permalink)
PaulChavez


 
 

Re: Simple sort text file

select uses a hastable with "Name" and "Expression", format-table and
format-list use a hashtable with "Label" and "Expression".


"Jason" wrote:
Quote:

> Beautiful! I was trying to use the "Expression" stuff, but the documentation
> I was following was using "Label" instead of name, and it never did work for
> me. This works great!
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re:simple text encrypt / decrypt VB Script
Sort text file PowerShell
How do I read a text file and sort text by fixed positions? PowerShell
Simple text document problem? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46