Sorting numbers from text files

June 13, 2010 at 04:02:52
Specs: Windows Vista
Hi,

Batch file problem
I have 3 text files called start, middle and end and each file has one row of numbers on one line and are of various sizes. The line starts with a space and then the number.
Eg
Start
1 23
Middle
2 3 6 7 10 11 12 15 16 19 20 21 24 25 26 27 28 31 32 35 36 37 38 39
End
4 8 13 17 22 29 33 40

I need away to read the numbers in the Start file and compare the numbers in the End file and return the numbers Eg

1 2 3 4 23 24 25 26 27 28 29

Then read these numbers into the original text file which has these numbers at the begining of each line and copy to a New file. The Middle file is properly not needed but I have shown it as I am not sure how to work this out.


See More: Sorting numbers from text files

Report •

#1
June 14, 2010 at 05:42:46
"read the numbers in the Start file and compare the numbers "

to what?


=====================================
Helping others achieve escape felicity

M2


Report •

#2
June 14, 2010 at 08:42:13
Assuming that the three files are called start.txt,
middle.txt, etc and they contain just one line of
numbers in each file then the following should do
the trick (btw it just echos the result to screen
as it is):

@echo off

setlocal EnableDelayedExpansion

set /p starttxt=<start.txt
set /p middletxt=<middle.txt
set /p endtxt=<end.txt

set n=0
for %%a in (%starttxt% %middletxt% %endtxt%) do (
  set /a n+=1
  set arr!n!=%%a
)
set str=
call :SelSort arr 1 %n%
call :FlagDupes arr 1 %n%
for /l %%i in (1, 1, %n%) do (
  if not defined str (
    if "!arr%%i!" neq "x" set str=!arr%%i!
  ) else (
    if "!arr%%i!" neq "x" set str=!str! !arr%%i!
  )
)
echo %str%
goto :eof

:FlagDupes (array_byref, lowarray_byval, higharray_byval)
for /l %%i in (%2, 1, %3) do (
  for /l %%j in (%2, 1, %3) do (
    if %%i neq %%j if !%1%%i! equ !%1%%j! set %1%%j=x
  )
)
goto :eof     

:SelSort (array_byref, lowarray_byval, higharray_byval)
set /a upper=%3 - 1
for /l %%i in (%2, 1, %upper%) do (
  set min=%%i
  set /a lower=%%i + 1
  for /l %%j in (!lower!, 1, %3) do (
    for %%m in (!min!) do (
      if !%1%%m! gtr !%1%%j! set min=%%j
    )
  )
  if !min! neq %%i (
    for %%m in (!min!) do (      
      set TmpVal=!%1%%i!
      set %1%%i=!%1%%m!
      set %1%%m=!TmpVal!
    )
  )
)


Report •

#3
June 14, 2010 at 11:04:37
Hi MarkLS

I ran your script but it returned all the numbers 1 2 3 4 upto 40. and what I need is to look at the first number in the Start file in this case 1 and then look in the End file for the next number larger than itself which is 4 and then return the numbers 1 2 3 4 and repeat the same procedure for the next number in the Start file 23 and find the next number in the End file 29 and then return the numbers 23 24 25 26 27 28 29 I hope this makes sense.


Report •

Related Solutions

#4
June 14, 2010 at 11:34:54
Hi Mechanix2Go

I hope I have answered your question in response 3. I do not think I need the Middle file as the number in the Start file will always be smaller than the number in the End file and the number in the End file will always be the first number found larger than the number in the Start file.
The Start file number will increment by 1 until it has found the number in the End file larger than its self.


Report •

#5
June 14, 2010 at 12:29:58
OK, I think I see what you mean. Try the following
(again it just echoes to the screen):

@echo off

setlocal EnableDelayedExpansion

set /p starttxt=<start.txt
set /p endtxt=<end.txt

set str=
for %%a in (%starttxt%) do (
  set HigherFound=false
  for %%b in (%endtxt%) do (
    if "!HigherFound!" equ "false" (
      if %%b gtr %%a (
        for /l %%i in (%%a, 1, %%b) do (
          if not defined str (
            set str=%%i
          ) else (
            set str=!str! %%i
          )
        )
        set HigherFound=true
      )
    )
  )
)
echo %str%


Report •

#6
June 14, 2010 at 13:33:20
Hi MarkLS

That's fantastic exactly what I wanted.
I now need to use those numbers to extract the lines of text from my original file to a newfile. My original file looks like this:-
1:Some text
2:More text
3:Even more
I would also like to remove the numbers and : when extracted to the new file. Also what changes are needed so I can run your script within my script without going to the screen
Good work


Report •

#7
June 15, 2010 at 05:05:15
If you replace the last line (echo %str%) in the code I posted in
response #5 with the code that follows it will get rid of the screen
output and retrieve the lines of text from your original file (you'll
need to replace occurrences of 'original.txt' and 'newfile.txt' with
the actual file names that you are using in this code).

type nul> newfile.txt
for /f "tokens=1* delims=:" %%a in (original.txt) do (
  for %%i in (%str%) do (
    if "%%i" equ "%%a" echo %%b>> newfile.txt
  )
)


Report •

#8
June 15, 2010 at 10:07:47
Hi MarkLS

Works like a dream
I have one more question if you do not mind at present I pickup on a word in my original file which I have entered into the coding and I was wondering if I could let the user enter the search word from the screen. The section of code is this:-
FINDSTR /N RED Colour.txt>>File1.txt
Also the option to enter the file name to search and repeat these options until the user has finished their task and output to screen and to a text file.


Report •

#9
June 16, 2010 at 05:15:54
Below is a complete [monotonous] program which you can
study and run - it should give you what you need as far as
getting user input is concerned. With regards to screen
and file output, basically: 'echo some text' goes to the
screen, 'echo sometext> somefile' goes to somefile and
'echo some text>> somefile' appends text to a file which
might already contain stuff.


@echo off

:input
set response=
set /p response=Please enter your favourite colour (or q to quit): 
if "%response%" equ "q" goto :eof
if defined response echo Your favourite colour is %response%
goto input


Report •

#10
June 19, 2010 at 04:38:00
Hi MarkLS,

Thank you for your help and I will now have to build on the info you have supplied and try sort out the : input in response 9 to get my input to keep appending to a file from my program which at present I keep getting the first input working fine but when a second input is entered my appended file repeats input 1 again eg line 1 is repeated at line 2 etc and then input 2 information is fine.
Thank you once again.


Report •

Ask Question