3

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.

adarshr
  • 61,315
  • 23
  • 138
  • 167
vladCovaliov
  • 4,333
  • 2
  • 43
  • 58

4 Answers4

6

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
)
jeb
  • 78,592
  • 17
  • 171
  • 225
1

May be too late for you but as a general advice you can do it much simpler than creating a temporary file. Just pipe it all through sort:

for /f "tokens=1-3" %%a in ('(for /f "tokens=1-3" %%x in (aux1.txt^) do @echo %%z %%x %%y^)^|sort') do echo %%b %%c %%a

Note, that it's a single command and can be used just by typing at command prompt without any batch file at all (have to reduce %%'s for that, of course).

panda-34
  • 4,089
  • 20
  • 25
0

While I really like Jeb's answer, Ive been using Chris LaRosa's TextDB tools for years.

You can sort multiple columns, pipe through another of his tools (or any other CL tool). Kind of old...

RobW
  • 424
  • 2
  • 5
0

SortFileBySecondColumn.bat

@echo off
if "%1"=="" (
echo Syntax: %0 FileToSort.txt [/R]
echo /R means Sort in Reverse Order
goto Ende
)
set InputFile=%1
for /f "tokens=1-2* delims= " %%a in ('(for /f "tokens=1-2* delims= " %%n in (%InputFile%^) do @echo %%o %%n %%p^)^|sort %2') do echo %%b %%a %%c
:Ende

Usage: SortFileBySecondColumn UnsortedFile.txt > SortedFile.txt
If you exchange delims= by delims=; then you can use it for CSV files.

Example Input file:

172.20.17.59 PC00000781 # 14.01.2022 12:35:55
172.20.17.22 NB00001021 # 31.01.2022 14:40:38
172.20.16.114 TRANSFER14 # 11.02.2022 11:22:07
10.59.80.27 PC00001034 # 14.02.2022 15:39:23
10.59.80.140 XXBNB00173 # 16.02.2022 10:13:31
172.20.17.23 PC00000XXX # 18.02.2022 12:40:58
10.59.232.25 NB00000178 # 18.02.2022 14:38:53

Resulting Output file:

10.59.232.25 NB00000178 # 18.02.2022 14:38:53 
172.20.17.22 NB00001021 # 31.01.2022 14:40:38 
172.20.17.59 PC00000781 # 14.01.2022 12:35:55 
172.20.17.23 PC00000XXX # 18.02.2022 12:40:58 
10.59.80.27 PC00001034 # 14.02.2022 15:39:23 
172.20.16.114 TRANSFER14 # 11.02.2022 11:22:07 
10.59.80.140 XXBNB00173 # 16.02.2022 10:13:31 
Michael Hutter
  • 1,064
  • 13
  • 33