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.

Anoop
  • 439
  • 2
  • 7
  • 12
  • what's `-n` supposed to be? – Stephan Aug 31 '15 at 06:50
  • It's for sorting numeric. – Anoop Aug 31 '15 at 06:52
  • windows `sort` does not have such a parameter (see `sort /?`) If you use another implementation of `sort`, please specify. – Stephan Aug 31 '15 at 06:54
  • I just used sort test.txt /o output.txt still its not working – Anoop Aug 31 '15 at 06:57
  • define "not working". – Stephan Aug 31 '15 at 07:00
  • 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 Aug 31 '15 at 07:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88344/discussion-between-anoop-and-stephan). – Anoop Aug 31 '15 at 07:01
  • found some possibly helpful answers: [1](http://stackoverflow.com/a/28078521/2152082), [2](http://stackoverflow.com/a/7115084/2152082), [3](http://stackoverflow.com/a/25647441/2152082) – Stephan Aug 31 '15 at 07:31
  • No positive response yet – Anoop Aug 31 '15 at 07:33

2 Answers2

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
Stephan
  • 53,940
  • 10
  • 58
  • 91
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)
Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • 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 Oct 06 '19 at 13:53
  • @Stephan This are using 2 delimiters, “: “ + 1 space! “: “ and I think the find is more fast and sort them findstr. – Io-oI Oct 06 '19 at 16:40
  • @Stephan let me know you are considering about it, please! – Io-oI Oct 06 '19 at 16:44
  • `"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. – Stephan Oct 06 '19 at 17:30