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:\Path\Document.txt | Select-Object -Skip 1
$emptyLine | Sort-Object | Out-File C:\Path\Document.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.
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)
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:\Path\Document.txt
@($First; $Rest |Sort-Object) |Set-Content C:\Path\Document.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:\Path\Document.txt