Batch IP Address sorting

In batch script, sort does not properly sort IP Addresses you get

192.168.1.1       192.168.1.2          192.168.1.12       192.168.1.3

Is it possible in batch script to sort IP Addresses correctly

192.168.1.1      192.168.1.2     192.168.1.3     192.168.1.12

ObDa answered my previous question about columns now need to get them in the right order
http://www.experts-exchange.com/questions/28687984/Batch-script-help.html#a40820410
crp0499CEOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
I guess you still want the column output?
@echo off
setlocal enabledelayedexpansion
set InputFile=D:\Temp\test.txt
set /a ColWidth = 3
set /a ColCount = 0
set TmpFile=%Temp%\%~n0.tmp
(for /f "tokens=1-4 delims=." %%a in ('type "%InputFile%"') do (
	set Oct1=00%%a
	set Oct2=00%%b
	set Oct3=00%%c
	set Oct4=00%%d
	echo !Oct1:~-3!!Oct2:~-3!!Oct3:~-3!!Oct4:~-3!	%%a.%%b.%%c.%%d
))>"%TmpFile%"

set Line=
for /f "tokens=2" %%a in ('type "%TmpFile%" ^| sort.exe') do (
	set /a ColCount += 1
	set Line=!Line!	%%a
	if !ColCount! geq !ColWidth! (
		set /a ColCount = 0
		echo.!Line:~1!
		set Line=
	)
)
if defined Line echo.%Line:~1%
del "%TmpFile%"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crp0499CEOAuthor Commented:
Yes, on the columns. thank you!

However, it's outputting like this now:

10.10.10.200     10.10.10.300     10.10.10.500
10.10.10.1          10.10.10.201     10.10.10.2

I do appreciate the help, I have been stuck on this for a week.
oBdACommented:
Can't reproduce this.
I used this as input file:
10.10.10.200
10.10.10.500
10.10.10.300
10.10.10.1
10.10.10.201
10.10.10.2

Open in new window

And got this as output:
10.10.10.1      10.10.10.2      10.10.10.200
10.10.10.201    10.10.10.300    10.10.10.500

Open in new window

Or do you have several addresses in one line?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

crp0499CEOAuthor Commented:
No, just one per line. I attached the file, see if you get the same result I do. Also I think I know what the issue is, when the script appends to the text file it leaves a space after the ip address before it goes to the next line. When I removed the spaces in the text I got the correct result. Can you compensate for this or fix the space the append leaves behind?
oBdACommented:
This should fix the issue with the trailing space:
@echo off
setlocal enabledelayedexpansion
set InputFile=D:\Temp\test.txt
set /a ColWidth = 3
set /a ColCount = 0
set TmpFile=%Temp%\%~n0.tmp
(for /f "tokens=1-4 delims=. " %%a in ('type "%InputFile%"') do (
	set Oct1=00%%a
	set Oct2=00%%b
	set Oct3=00%%c
	set Oct4=00%%d
	echo !Oct1:~-3!!Oct2:~-3!!Oct3:~-3!!Oct4:~-3!	%%a.%%b.%%c.%%d
))>"%TmpFile%"

set Line=
for /f "tokens=2" %%a in ('type "%TmpFile%" ^| sort.exe') do (
	set /a ColCount += 1
	set Line=!Line!	%%a
	if !ColCount! geq !ColWidth! (
		set /a ColCount = 0
		echo.!Line:~1!
		set Line=
	)
)
if defined Line echo.%Line:~1%
del "%TmpFile%"

Open in new window

crp0499CEOAuthor Commented:
Yes that did the trick.. I see that you added a space in the delimiters, great thinking. I guess when you look at code all day this things can be over looked. Thank you!

I have one last question if you can answer it real quick. When I put your script into my script, I lose the tabs. Where in your script do the tabs come into play?
oBdACommented:
What exactly do you mean with "lose the tabs"? Since I don't know what your script looks like and where you put my script, it's a bit hard to determine what goes wrong.
I'm using tabs as field separators for the columns, for example (line 18).
crp0499CEOAuthor Commented:
Thank you! Sorry it took so long, I found that where I put the code, I put a space and not a TAB between SET Line=!Line! %%a.

So before I was getting
10.10.10.1 10.10.10.2 10.10.10.3
10.10.10.4 10.10.10.200 10.10.10.201

Instead of

10.10.10.1     10.10.10.2      10.10.10.3
10.10.10.4     10.10.10.200  10.10.10.201

Thank you, you have been a great help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.