≡ Menu

Sort command

We can sort a text file using the windows inbuilt sort command. Below you can find the syntax of sort command.

Sort a text file:

A text file can be sorted using the below simple command.

sort filename

For example, to sort the file data.txt, the command would be
sort data.txt

The above command prints the sorted contents of the file in the console. To save the output into another file, you can use
/o switch as shown below.

sort filename /o outputfile

Example:

sort data.txt /o sorteddata.txt

Alternatively, you can user redirection operator.

sort data.txt > sorteddata.txt

Advanced options for sorting:

Sort the contents in reverse order

sort /R filename /o outputfile

If you are sorting big files, then /M switch will help you to finish the sorting quickly. Be default, sort command uses only 160 KB of space to store the file contents in main memory. Increasing this limit, will increase the performance of the sort operation. To let the sort command use 10MB of memory, we can run the below command.

sort /M 10240 data.txt /o sorteddata.txt
9 comments… add one
  • Stero

    I think the /M parameter is stated incorrectly here, please see the command line help info about /M:

    /M[EMORY] kilobytes Specifies amount of main memory to use for
    the sort, in kilobytes. The memory size is
    always constrained to be a minimum of 160
    kilobytes. If the memory size is specified
    the exact amount will be used for the sort,
    regardless of how much main memory is
    available.

    The best performance is usually achieved by
    not specifying a memory size. By default the
    sort will be done with one pass (no temporary
    file) if it fits in the default maximum
    memory size, otherwise the sort will be done
    in two passes (with the partially sorted data
    being stored in a temporary file) such that
    the amounts of memory used for both the sort
    and merge passes are equal. The default
    maximum memory size is 90% of available main
    memory if both the input and output are
    files, and 45% of main memory otherwise.

    If /M not specified, sort will try to utilize the as mush memory as possible, under this condition, the 160kb is the minimum memory not the maximum. If you specified /M, it will use that amount of memory exactly.

  • God

    This doesn’t work on Windows 7.
    Looks like the switches have disappeared.
    “help sort” just produces an error now too.

  • Leo

    To “God”, try this in Win7:

    sort /?

  • CmdrDick

    Works fine in Win7
    From All Programs, accessories, “command prompt”
    Command box will appear
    for directions type sort /?
    sort c:\infile.txt /O c:\outfile.txt
    May need to create an empty output file due to win restriction on writing files to c:\ without administrator permission

  • Scott

    Is there a way to to sort numerically? For example instead of:
    10
    101
    20
    It would sort numerically like:
    10
    20
    101

    • to “scott”.
      to sort numerically, you’ll have to add leading zeros to numbers to make them of the same length based on the largest in your list

      say you have the list,
      10
      35
      20
      96
      172
      1268
      936
      left pad the numbers with zeros based on the largest “1268” then you get
      0010
      0035
      0020
      0096
      0172
      1268
      0936
      now applying sort would yield a numerical result
      0010
      0020
      0035
      0096
      0172
      0936
      1268

    • asdf

      for achieving a numerically sound sort in ascending or descending order ( with /r switch) one must first append leading zeros to all numbers based on the largest number in the list
      say, you have the list
      10 , 100, 30, 25, 1436
      the largest is 1436. so, each number must have leading zeros to make them 4 digits long
      0010, 0100, 0030, 0025, 1436
      then applying the sort would yield
      0010, 0025, 0030, 0100, 1436
      this needs two sorts. one, to find out largest. two, to sort the list. (not mentioning the appending to do after finding the largest)

      in practice, if you already know the largest number of digits you would encounter in your problem then just do this
      set paddedValue=00000000%number%
      echo %paddedValue~-LargestNumberOfDigitsYouWouldPossiblyEncounter% >> someTempFile.txt
      then
      sort someTempFile.txt
      this is a lot simpler but requires you to foresee the largest digit number you would encounter

    • someBatDev

      just do this
      set paddedNumber=000000000%number% (its 9 zeros)
      echo %paddedNumber:~-9% >> someTemp.txt
      sort someTemp.txt
      this works as the numbers are 32 bit so you get 2.147 billion as limit. im using upto 9 digits so just 1 shy of a billion (if you use 10 then it could easily cross over the 2.147 billion limit). but if your numbers are crossing that limit then you should consider solving your problems by other means. a bat file can only do so much

  • Greg

    Hi,

    Is there a way to just remove duplicates without sorting the file?

    Thanks
    Greg

Cancel reply

Leave a Comment