Batch sort by column in file

batch-file, file, sorting

Solution

You could write your own sort-wrapper with a batch file.

You only need to reorder the columns into a temporary file, sort it and order it back. (nearly obvious)

REM *** Get the desired colum and place it as first column in the temporary file
setlocal DisableDelayedExpansion
(
    for /F "tokens=1-3 delims= " %%A in (aux1.txt) DO (
        set "par1=%%A"
        set "par2=%%B"
        set "par3=%%C"
        setlocal EnableDelayedExpansion
        echo(!par2! !par1! !par2! !par3!
        endlocal
  )
) > aux1.txt.tmp

REM ** Now sort the first colum, but echo only the rest of the line
for /F "usebackq tokens=1,* delims= " %%A in (`sort /r aux1.txt.tmp`) DO (
    echo(%%B
)

Problem

I wonder if there is any posibility to sort a text file by columns. For example I have `aux1.txt` with rows like this ``` Name SecondName Grade ``` In shell i can do this ``` sort -r -k 3 aux1 ``` It sorts the file by the 3rd column(grade). In batch ``` sort /+3 < aux1.txt ``` sorts the file after the 3rd letter. I read the sort manual for batch but no results.

Original source