Powershell sort from line 2
I want to sort a .txt document, so that the first line stays on top, and the rest gets sorted by numbers.
I currently have the following, but it removes the first line, and the sorting isn't correct, since the numbers are e.g. 72, 2, 51, 100, 201, 5 etc.
so it sorts it like 100, 2, 201, 5, 51, 72.
And I wish to have it sorted like 2, 5, 51, 72, 100, 201
$emptyLine = Get-Content C:PathDocument.txt | Select-Object -Skip 1
$emptyLine | Sort-Object | Out-File C:PathDocument.txt
I know this is possible in Notepad++ but the machine that needs to do the sorting isn't allowed to install anything new.
I'm very open if for any solution, that doesn't require me to install a new piece of software.
The machine is Windows 10 Pro x64.
Current Picture
Desired result
Current Result
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
(Posted as HTML snippet, to make formatting more beautiful)
powershell sort-object
add a comment |
I want to sort a .txt document, so that the first line stays on top, and the rest gets sorted by numbers.
I currently have the following, but it removes the first line, and the sorting isn't correct, since the numbers are e.g. 72, 2, 51, 100, 201, 5 etc.
so it sorts it like 100, 2, 201, 5, 51, 72.
And I wish to have it sorted like 2, 5, 51, 72, 100, 201
$emptyLine = Get-Content C:PathDocument.txt | Select-Object -Skip 1
$emptyLine | Sort-Object | Out-File C:PathDocument.txt
I know this is possible in Notepad++ but the machine that needs to do the sorting isn't allowed to install anything new.
I'm very open if for any solution, that doesn't require me to install a new piece of software.
The machine is Windows 10 Pro x64.
Current Picture
Desired result
Current Result
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
(Posted as HTML snippet, to make formatting more beautiful)
powershell sort-object
1
Can you provide the sample input?
– Vivek Kumar Singh
Nov 20 at 10:46
1
Heine, Welcome to Stackoverflow, to get better and quicker results on your questions (and save the answering people some time), I recommend you add (input/desired/current) listings in text format rather than pictures.
– iRon
Nov 20 at 12:40
Hi, thank you. Oh yeah, makes sense to me! Sorry about that, I will keep that in mind!
– Heine Kristensen
Nov 20 at 12:51
@Heine, I have changed my answer in respect to the given source table
– iRon
Nov 20 at 14:44
add a comment |
I want to sort a .txt document, so that the first line stays on top, and the rest gets sorted by numbers.
I currently have the following, but it removes the first line, and the sorting isn't correct, since the numbers are e.g. 72, 2, 51, 100, 201, 5 etc.
so it sorts it like 100, 2, 201, 5, 51, 72.
And I wish to have it sorted like 2, 5, 51, 72, 100, 201
$emptyLine = Get-Content C:PathDocument.txt | Select-Object -Skip 1
$emptyLine | Sort-Object | Out-File C:PathDocument.txt
I know this is possible in Notepad++ but the machine that needs to do the sorting isn't allowed to install anything new.
I'm very open if for any solution, that doesn't require me to install a new piece of software.
The machine is Windows 10 Pro x64.
Current Picture
Desired result
Current Result
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
(Posted as HTML snippet, to make formatting more beautiful)
powershell sort-object
I want to sort a .txt document, so that the first line stays on top, and the rest gets sorted by numbers.
I currently have the following, but it removes the first line, and the sorting isn't correct, since the numbers are e.g. 72, 2, 51, 100, 201, 5 etc.
so it sorts it like 100, 2, 201, 5, 51, 72.
And I wish to have it sorted like 2, 5, 51, 72, 100, 201
$emptyLine = Get-Content C:PathDocument.txt | Select-Object -Skip 1
$emptyLine | Sort-Object | Out-File C:PathDocument.txt
I know this is possible in Notepad++ but the machine that needs to do the sorting isn't allowed to install anything new.
I'm very open if for any solution, that doesn't require me to install a new piece of software.
The machine is Windows 10 Pro x64.
Current Picture
Desired result
Current Result
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
(Posted as HTML snippet, to make formatting more beautiful)
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
powershell sort-object
powershell sort-object
edited Nov 20 at 13:48
asked Nov 20 at 10:44
Heine Kristensen
84
84
1
Can you provide the sample input?
– Vivek Kumar Singh
Nov 20 at 10:46
1
Heine, Welcome to Stackoverflow, to get better and quicker results on your questions (and save the answering people some time), I recommend you add (input/desired/current) listings in text format rather than pictures.
– iRon
Nov 20 at 12:40
Hi, thank you. Oh yeah, makes sense to me! Sorry about that, I will keep that in mind!
– Heine Kristensen
Nov 20 at 12:51
@Heine, I have changed my answer in respect to the given source table
– iRon
Nov 20 at 14:44
add a comment |
1
Can you provide the sample input?
– Vivek Kumar Singh
Nov 20 at 10:46
1
Heine, Welcome to Stackoverflow, to get better and quicker results on your questions (and save the answering people some time), I recommend you add (input/desired/current) listings in text format rather than pictures.
– iRon
Nov 20 at 12:40
Hi, thank you. Oh yeah, makes sense to me! Sorry about that, I will keep that in mind!
– Heine Kristensen
Nov 20 at 12:51
@Heine, I have changed my answer in respect to the given source table
– iRon
Nov 20 at 14:44
1
1
Can you provide the sample input?
– Vivek Kumar Singh
Nov 20 at 10:46
Can you provide the sample input?
– Vivek Kumar Singh
Nov 20 at 10:46
1
1
Heine, Welcome to Stackoverflow, to get better and quicker results on your questions (and save the answering people some time), I recommend you add (input/desired/current) listings in text format rather than pictures.
– iRon
Nov 20 at 12:40
Heine, Welcome to Stackoverflow, to get better and quicker results on your questions (and save the answering people some time), I recommend you add (input/desired/current) listings in text format rather than pictures.
– iRon
Nov 20 at 12:40
Hi, thank you. Oh yeah, makes sense to me! Sorry about that, I will keep that in mind!
– Heine Kristensen
Nov 20 at 12:51
Hi, thank you. Oh yeah, makes sense to me! Sorry about that, I will keep that in mind!
– Heine Kristensen
Nov 20 at 12:51
@Heine, I have changed my answer in respect to the given source table
– iRon
Nov 20 at 14:44
@Heine, I have changed my answer in respect to the given source table
– iRon
Nov 20 at 14:44
add a comment |
2 Answers
2
active
oldest
votes
Take advantage of multi-value assignment and store the output from Get-Content
in two variables, then sort only the second:
$First,$Rest = Get-Content C:PathDocument.txt
@($First; $Rest |Sort-Object) |Set-Content C:PathDocument.txt
In the scenario above, PowerShell will assign the first line to $First
and the rest to $Rest
If you need Sort-Object
to sort by the first part of a string property as if it was an integer, you could do it like this:
@($First; $Rest |Sort-Object {(-split $_)[0] -as [int]}) |Set-Content C:PathDocument.txt
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
You might use Get-Content
, skip the header and than do some tricks to rebuild your table but you might also consider using the ConvertFrom-SourceTable
from the PowerShell Gallery to convert your table directly into objects:
$List = Get-Content C:PathDocument.txt | ConvertFrom-SourceTable
Unfortunately the numbers in the first column appear to be left aligned, if they where right aligned (as in the final results of this answer), the ConvertFrom-SourceTable
cmdlet would have automatically evaluated the first 'No
' column into numbers.
This means that you will need to transfer the first column into integers yourself to be able to sort them as suggested. You can do this by using the Select-Object
and creating a calculated property:
Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part
After that, you can simply use sort-Object
to sort it:
Sort-Object No
Putting an example together:
ConvertFrom-SourceTable '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
This example will result in:
No Tool_Name Tool_Part
-- --------- ---------
43 36 29. 1
50 36 15.9 1
64 2 6. 1
72 10 9. 1
73 10 5.3 1
75 3 5.5 1
76 3 5.3 1
102 34 1.7 1
251 2 3. 1
600 33 3. 1
As it is unclear whether the column between Tool_Name
and Tool_Part
belongs to either of the column (because Tool_Part
could be either left aligned or right aligned), you might provide a ruler to the ConvertFrom-SourceTable
yourself to settle this:
ConvertFrom-SourceTable -Ruler '--- ----------- ---------' '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
Result:
No Tool_Name Tool_Part
-- --------- ---------
43 36 3 29. 1
50 36 3 15.9 1
64 2 1 6. 1
72 10 1 9. 1
73 10 1 5.3 1
75 3 1 5.5 1
76 3 1 5.3 1
102 34 1 1.7 1
251 2 3 3. 1
600 33 1 3. 1
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391265%2fpowershell-sort-from-line-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Take advantage of multi-value assignment and store the output from Get-Content
in two variables, then sort only the second:
$First,$Rest = Get-Content C:PathDocument.txt
@($First; $Rest |Sort-Object) |Set-Content C:PathDocument.txt
In the scenario above, PowerShell will assign the first line to $First
and the rest to $Rest
If you need Sort-Object
to sort by the first part of a string property as if it was an integer, you could do it like this:
@($First; $Rest |Sort-Object {(-split $_)[0] -as [int]}) |Set-Content C:PathDocument.txt
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
Take advantage of multi-value assignment and store the output from Get-Content
in two variables, then sort only the second:
$First,$Rest = Get-Content C:PathDocument.txt
@($First; $Rest |Sort-Object) |Set-Content C:PathDocument.txt
In the scenario above, PowerShell will assign the first line to $First
and the rest to $Rest
If you need Sort-Object
to sort by the first part of a string property as if it was an integer, you could do it like this:
@($First; $Rest |Sort-Object {(-split $_)[0] -as [int]}) |Set-Content C:PathDocument.txt
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
Take advantage of multi-value assignment and store the output from Get-Content
in two variables, then sort only the second:
$First,$Rest = Get-Content C:PathDocument.txt
@($First; $Rest |Sort-Object) |Set-Content C:PathDocument.txt
In the scenario above, PowerShell will assign the first line to $First
and the rest to $Rest
If you need Sort-Object
to sort by the first part of a string property as if it was an integer, you could do it like this:
@($First; $Rest |Sort-Object {(-split $_)[0] -as [int]}) |Set-Content C:PathDocument.txt
Take advantage of multi-value assignment and store the output from Get-Content
in two variables, then sort only the second:
$First,$Rest = Get-Content C:PathDocument.txt
@($First; $Rest |Sort-Object) |Set-Content C:PathDocument.txt
In the scenario above, PowerShell will assign the first line to $First
and the rest to $Rest
If you need Sort-Object
to sort by the first part of a string property as if it was an integer, you could do it like this:
@($First; $Rest |Sort-Object {(-split $_)[0] -as [int]}) |Set-Content C:PathDocument.txt
edited Nov 20 at 17:07
answered Nov 20 at 11:00
Mathias R. Jessen
56.1k455101
56.1k455101
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
I think that the issue is that the list needs to be sorted on the integer value of the first column and not the whole line (string)...
– iRon
Nov 20 at 12:29
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
This is what works the best! Thank you! Is it possible to make it recognize the lack of 002 instead of 2?
– Heine Kristensen
Nov 20 at 13:17
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Sorry, completely missed that part @HeineKristensen - updated the answer
– Mathias R. Jessen
Nov 20 at 17:07
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
Amazing! Thank you so much!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
You might use Get-Content
, skip the header and than do some tricks to rebuild your table but you might also consider using the ConvertFrom-SourceTable
from the PowerShell Gallery to convert your table directly into objects:
$List = Get-Content C:PathDocument.txt | ConvertFrom-SourceTable
Unfortunately the numbers in the first column appear to be left aligned, if they where right aligned (as in the final results of this answer), the ConvertFrom-SourceTable
cmdlet would have automatically evaluated the first 'No
' column into numbers.
This means that you will need to transfer the first column into integers yourself to be able to sort them as suggested. You can do this by using the Select-Object
and creating a calculated property:
Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part
After that, you can simply use sort-Object
to sort it:
Sort-Object No
Putting an example together:
ConvertFrom-SourceTable '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
This example will result in:
No Tool_Name Tool_Part
-- --------- ---------
43 36 29. 1
50 36 15.9 1
64 2 6. 1
72 10 9. 1
73 10 5.3 1
75 3 5.5 1
76 3 5.3 1
102 34 1.7 1
251 2 3. 1
600 33 3. 1
As it is unclear whether the column between Tool_Name
and Tool_Part
belongs to either of the column (because Tool_Part
could be either left aligned or right aligned), you might provide a ruler to the ConvertFrom-SourceTable
yourself to settle this:
ConvertFrom-SourceTable -Ruler '--- ----------- ---------' '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
Result:
No Tool_Name Tool_Part
-- --------- ---------
43 36 3 29. 1
50 36 3 15.9 1
64 2 1 6. 1
72 10 1 9. 1
73 10 1 5.3 1
75 3 1 5.5 1
76 3 1 5.3 1
102 34 1 1.7 1
251 2 3 3. 1
600 33 1 3. 1
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
You might use Get-Content
, skip the header and than do some tricks to rebuild your table but you might also consider using the ConvertFrom-SourceTable
from the PowerShell Gallery to convert your table directly into objects:
$List = Get-Content C:PathDocument.txt | ConvertFrom-SourceTable
Unfortunately the numbers in the first column appear to be left aligned, if they where right aligned (as in the final results of this answer), the ConvertFrom-SourceTable
cmdlet would have automatically evaluated the first 'No
' column into numbers.
This means that you will need to transfer the first column into integers yourself to be able to sort them as suggested. You can do this by using the Select-Object
and creating a calculated property:
Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part
After that, you can simply use sort-Object
to sort it:
Sort-Object No
Putting an example together:
ConvertFrom-SourceTable '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
This example will result in:
No Tool_Name Tool_Part
-- --------- ---------
43 36 29. 1
50 36 15.9 1
64 2 6. 1
72 10 9. 1
73 10 5.3 1
75 3 5.5 1
76 3 5.3 1
102 34 1.7 1
251 2 3. 1
600 33 3. 1
As it is unclear whether the column between Tool_Name
and Tool_Part
belongs to either of the column (because Tool_Part
could be either left aligned or right aligned), you might provide a ruler to the ConvertFrom-SourceTable
yourself to settle this:
ConvertFrom-SourceTable -Ruler '--- ----------- ---------' '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
Result:
No Tool_Name Tool_Part
-- --------- ---------
43 36 3 29. 1
50 36 3 15.9 1
64 2 1 6. 1
72 10 1 9. 1
73 10 1 5.3 1
75 3 1 5.5 1
76 3 1 5.3 1
102 34 1 1.7 1
251 2 3 3. 1
600 33 1 3. 1
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
You might use Get-Content
, skip the header and than do some tricks to rebuild your table but you might also consider using the ConvertFrom-SourceTable
from the PowerShell Gallery to convert your table directly into objects:
$List = Get-Content C:PathDocument.txt | ConvertFrom-SourceTable
Unfortunately the numbers in the first column appear to be left aligned, if they where right aligned (as in the final results of this answer), the ConvertFrom-SourceTable
cmdlet would have automatically evaluated the first 'No
' column into numbers.
This means that you will need to transfer the first column into integers yourself to be able to sort them as suggested. You can do this by using the Select-Object
and creating a calculated property:
Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part
After that, you can simply use sort-Object
to sort it:
Sort-Object No
Putting an example together:
ConvertFrom-SourceTable '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
This example will result in:
No Tool_Name Tool_Part
-- --------- ---------
43 36 29. 1
50 36 15.9 1
64 2 6. 1
72 10 9. 1
73 10 5.3 1
75 3 5.5 1
76 3 5.3 1
102 34 1.7 1
251 2 3. 1
600 33 3. 1
As it is unclear whether the column between Tool_Name
and Tool_Part
belongs to either of the column (because Tool_Part
could be either left aligned or right aligned), you might provide a ruler to the ConvertFrom-SourceTable
yourself to settle this:
ConvertFrom-SourceTable -Ruler '--- ----------- ---------' '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
Result:
No Tool_Name Tool_Part
-- --------- ---------
43 36 3 29. 1
50 36 3 15.9 1
64 2 1 6. 1
72 10 1 9. 1
73 10 1 5.3 1
75 3 1 5.5 1
76 3 1 5.3 1
102 34 1 1.7 1
251 2 3 3. 1
600 33 1 3. 1
You might use Get-Content
, skip the header and than do some tricks to rebuild your table but you might also consider using the ConvertFrom-SourceTable
from the PowerShell Gallery to convert your table directly into objects:
$List = Get-Content C:PathDocument.txt | ConvertFrom-SourceTable
Unfortunately the numbers in the first column appear to be left aligned, if they where right aligned (as in the final results of this answer), the ConvertFrom-SourceTable
cmdlet would have automatically evaluated the first 'No
' column into numbers.
This means that you will need to transfer the first column into integers yourself to be able to sort them as suggested. You can do this by using the Select-Object
and creating a calculated property:
Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part
After that, you can simply use sort-Object
to sort it:
Sort-Object No
Putting an example together:
ConvertFrom-SourceTable '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
This example will result in:
No Tool_Name Tool_Part
-- --------- ---------
43 36 29. 1
50 36 15.9 1
64 2 6. 1
72 10 9. 1
73 10 5.3 1
75 3 5.5 1
76 3 5.3 1
102 34 1.7 1
251 2 3. 1
600 33 3. 1
As it is unclear whether the column between Tool_Name
and Tool_Part
belongs to either of the column (because Tool_Part
could be either left aligned or right aligned), you might provide a ruler to the ConvertFrom-SourceTable
yourself to settle this:
ConvertFrom-SourceTable -Ruler '--- ----------- ---------' '
No Tool_Name Tool_Part
72 10 1 9. 1
43 36 3 29. 1
102 34 1 1.7 1
600 33 1 3. 1
76 3 1 5.3 1
75 3 1 5.5 1
251 2 3 3. 1
73 10 1 5.3 1
50 36 3 15.9 1
64 2 1 6. 1
' | Select-Object @{n='No'; e={[Int]$_.No}}, Tool_Name, Tool_Part | Sort-Object No
Result:
No Tool_Name Tool_Part
-- --------- ---------
43 36 3 29. 1
50 36 3 15.9 1
64 2 1 6. 1
72 10 1 9. 1
73 10 1 5.3 1
75 3 1 5.5 1
76 3 1 5.3 1
102 34 1 1.7 1
251 2 3 3. 1
600 33 1 3. 1
edited Nov 20 at 14:57
answered Nov 20 at 12:27
iRon
3,90822234
3,90822234
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
This is really great, but I cannot format the table, since it is a list that some CNC machine is reading from, and it generates the formatting of the table itself :(. But Thanks, I will most likely be needing this in the future!
– Heine Kristensen
Nov 21 at 9:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391265%2fpowershell-sort-from-line-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you provide the sample input?
– Vivek Kumar Singh
Nov 20 at 10:46
1
Heine, Welcome to Stackoverflow, to get better and quicker results on your questions (and save the answering people some time), I recommend you add (input/desired/current) listings in text format rather than pictures.
– iRon
Nov 20 at 12:40
Hi, thank you. Oh yeah, makes sense to me! Sorry about that, I will keep that in mind!
– Heine Kristensen
Nov 20 at 12:51
@Heine, I have changed my answer in respect to the given source table
– iRon
Nov 20 at 14:44