I wanted to sort numbers in a text file and write the same in another text file for the same I am using the command given below but I am not able to get the output can someone please help me with the same

sort -n test.txt /o output.txt

There is an error message coming up for this as Input file specified two times.

upvote
  flag
what's -n supposed to be? – Stephan
upvote
  flag
It's for sorting numeric. – Anoop
upvote
  flag
windows sort does not have such a parameter (see sort /?) If you use another implementation of sort, please specify. – Stephan
upvote
  flag
I just used sort test.txt /o output.txt still its not working – Anoop
upvote
  flag
define "not working". – Stephan
upvote
  flag
Its giving wrong result my text is 1 100 101 103 107 110 130 131 132 133 134 135 149 152 153 16 17 18 194 195 196 197 198 199 2 200 201 202 224 244 245 246 256 257 258 259 26 260 261 262 263 264 272 273 274 275 28 29 301 32 344 349 350 379 380 388 403 404 411 52 53 85 86 87 90 91 94 97 – Anoop
upvote
  flag
upvote
  flag
found some possibly helpful answers: 1, 2, 3 – Stephan
upvote
  flag
No positive response yet – Anoop

1 Answers 11

(nice exercise :) )

@echo off
setlocal enabledelayedexpansion

REM convert all numbers to the same lenght by adding leading spaces:
(for /f %%i in (input.txt) do (
    REM 12 leading spaces:
    set "Z=            %%i"
    REM take last 12 digits:
    echo !Z:~-12!
))>temp.txt 

REM sort it:'
sort temp.txt /O temp.txt

REM remove the spaces:
(for /f %%i in (temp.txt) do echo/%%i)>output.txt
del temp.txt
upvote
  flag
This is not working for me – Anoop
upvote
  flag
define "not working"... Working well here (despite of a typo (corrected)) – Stephan
upvote
  flag
I passed a temp file and the output file is blank – Anoop
upvote
  flag
you are supposed to pass input.txt. temp.txt is (over)written by the code. – Stephan
upvote
  flag
Thanks it works for me now – Anoop

Not the answer you're looking for? Browse other questions tagged or ask your own question.