1

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.

9
  • what's -n supposed to be? Commented Aug 31, 2015 at 6:50
  • It's for sorting numeric. Commented Aug 31, 2015 at 6:52
  • windows sort does not have such a parameter (see sort /?) If you use another implementation of sort, please specify. Commented Aug 31, 2015 at 6:54
  • I just used sort test.txt /o output.txt still its not working Commented Aug 31, 2015 at 6:57
  • define "not working". Commented Aug 31, 2015 at 7:00

2 Answers 2

0

(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
Sign up to request clarification or add additional context in comments.

3 Comments

define "not working"... Working well here (despite of a typo (corrected))
I passed a temp file and the output file is blank
you are supposed to pass input.txt. temp.txt is (over)written by the code.
0

For sure (nice exercise :) ) !!!


Sort numbers in text file using command prompt but not using sort command!!**

@echo off && setlocal enabledelayedexpansion & cd /d "%~dp0" 

for /f delims^=:^ tokens^=^2 %%i in ('find /c /v ";" input.txt')do set _cnt=%%i
2>nul (cd.>"%tmp%\_file_.tmp" & cd.>"output.txt") & for /f %%i in (input.txt)do echo/,%%i,>>"%tmp%\_file_.tmp"
for /l %%L in (1 1 99999)do find ",%%L," "%tmp%\_file_.tmp" >nul && (echo/%%L>>"output.txt" & call set /a "_cnt-=1" && if !_cnt! == 0 goto :~0)

:~0  
timeout -1  & del "%tmp%\_file_.tmp" & type "output.txt"

:: to count lines / numbers that exist in the file, too, to be used as a run delimiter in for /l ::
for /f delims^=:^ tokens^=^2 %%i in ('find /c /v ";" input.txt')do set _cnt=%%i

:: create the 2 empty files and if they exist, delete the contents ::
2>nul (cd.>"%tmp%\_file_.tmp" & cd.>"output.txt")

:: create unique string for each file number/line "%tmp%\_file_.tmp" ::
for /f %%i in (input.txt)do echo/,%%i,>>"%tmp%\_file_.tmp"

rem :: performs a 1 in 1 to 99999 numeric loop and also finds ",number," in the file "%tmp%\_file_.tmp" ::
rem :: remove ",," (commas) from string, save (if any), only the variable number %%L in "output.txt"
for /l %%L in (1 1 99999)do find ",%%L," "%tmp%\_file_.tmp" >nul && (echo/%%L>>"output.txt" & call set /a "_cnt-=1" && if !_cnt! == 0 goto :~0)

:: for each occurrence, it will be subtracting 1 from the _cnt counter, so when it reaches zero it stops listing/fetching ::
echo/%%L>>"output.txt" & call set /a "_cnt-=1" && if !_cnt! == 0 goto :~0)

4 Comments

Instead of delims^=:^ tokens^=^2 you can just do "tokens=2 delims=:" (much easier to read). And you don't need commas if you use findstr /x (or maybe findstr "\<%%L\>" to be safe from stray spaces)
@Stephan This are using 2 delimiters, “: “ + 1 space! “: “ and I think the find is more fast and sort them findstr.
@Stephan let me know you are considering about it, please!
"tokens=2 delims=: " then. I don't know, if find is faster than findstr, but if it is, the generation of _file_.tmp surely negates the advantage.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.