Search code examples
sortingbatch-filecmd

How to use sort command in batch and sort over /+9


I´m working on a school project where one task is to write a batch script that sort columns from a text file.

example text file:

078 Hyllplan    1200    100 30  2
077 Bokhylla    5000    120 30  80
096 Skap        15000   80  40  85
146 VitGarderob 9000    80  60  200
100 Bankskiva   3650    180 100 5
163 Skrivbord   25800   120 60  70
182 Pelarbord   1600    60  60  70

Now to the problem, its works fine to sort the two first columns but not the others. How can I sort for example the third column(sort /+12)and so on


Solution

  • One option might be to use PowerShell in a batch-file. If you are on a supported Windows system, PowerShell will be available.

    This will produce a "strict" CSV file with TAB delimiters. Microsoft's view of a "strict" CSV file is that fields are quoted.

    powershell -NoLogo -NoProfile -Command ^
        "Get-Content -Path .\so66227463-447901.txt |" ^
            "ConvertFrom-Csv -Delimiter "`t" -Header @('key','name','n1','n2','n3','n4') |" ^
            "Sort-Object {[int]$_.n1} |" ^
            "ConvertTo-Csv -NoTypeInformation -Delimiter "`t" |" ^
            "Select-Object -Skip 1 |" ^
            "Out-File -FilePath '.\sorted.txt' -Encoding ascii"
    

    If you want an unquoted output file, you might investigate https://stackoverflow.com/a/63462493/447901