-1

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

Djo
  • 3
  • 3
  • 3
    Using `/+9` means to `start` sorting at the ninth character. The ninth character in the posted sample data is in the middle of a brand name. What do you want to sort on? Please read the output of the command `SORT /?` carefully – lit Feb 16 '21 at 16:07
  • Are you trying to sort FIXED or DELIMITED text? – Squashman Feb 16 '21 at 16:10
  • @Squashman i dont really know want you mean by that but the first column [3 numbers], second column [4-12 letters], third [4-5 numbers], fourth, fifth and the last [1-3 numbers] – Djo Feb 16 '21 at 16:18
  • The "example text file" posted in the question has fixed field colums of `0-3, 4-15, 16-23, 24-27, 28-31, 32-`. On which column does the sort need to be done? – lit Feb 16 '21 at 16:22
  • Is the whitespace in the sample data tabs or spaces? – SomethingDark Feb 16 '21 at 16:25
  • Please copy and paste as text into the question the output of the following command. `powershell -NoLogo -NoProfile -Command "Format-Hex -Path example-file.txt"` – lit Feb 16 '21 at 16:32
  • @lit I have made functions that let the user choose, for example press 1 to sort column 1, press 2 for column 2 and so on. – Djo Feb 16 '21 at 16:45
  • @SomethingDark tabs – Djo Feb 16 '21 at 16:45
  • @Djo fixed text files are where fields of a file start and stop at a specific byte position. They were very prevalent back in the days of mainframes. Let's just use a mailing address for example. Addresses can be very long, so in my world on the mainframe, we allocate the first 50 characters of the line to be the street. Then we have the city that comes next. The city field is assigned 30 characters. The state field only 2 characters and the person's zip code get 5 characters in the United States. So this makes each line 87 bytes long. Address 1-50 City 51-80 State 81-82 Zipcode 83-87. – Squashman Feb 16 '21 at 16:48
  • @Djo the Windows SORT command cannot sort based on a defined field that is delimited. If it could you would have an option in the help file that would say so. The SORT /+ option is very clear that it starts at a character position and since your fields most likely do not have trailing spaces you cannot sort by character position. – Squashman Feb 16 '21 at 16:51
  • @Djo if you want to sort by a defined delimited field you have a few options. You can use [JSORT.BAT](https://www.dostips.com/forum/viewtopic.php?t=5595) which is a hybrid jscript batch file. All the sorting is done in the jscript and it has options to sort by delimited fields and positions within those fields. Your other option would be Powershell. – Squashman Feb 16 '21 at 16:53

1 Answers1

0

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

lit
  • 14,456
  • 10
  • 65
  • 119
  • I feel like you could just iterate over the file with a `for /f "delims="` loop, then `set "line=%%A"` and then `set line=!line:=","!` and finally `>>output.csv echo "!line!"` instead of needing all that powershell. – SomethingDark Feb 16 '21 at 18:32
  • @SomethingDark, that could be done, but it does not appear to do any sorting. Besides, PowerShell is Microsoft's stated strategy for the future. Might as well get with the program. – lit Feb 16 '21 at 23:08
  • Well, no, you'd have to sort it afterwards, but it saves a ton of typing for the conversion bit. – SomethingDark Feb 17 '21 at 00:56