![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||