Manipulate data

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Manipulate data

#1 Post by alexandredneto » 13 Aug 2013 10:57

Hi, guys.

I want the value %%m is the IP hostname %SERVER%

Code: Select all

SET SERVER = www.google.com
for / f "delims ="%% m in ('ping-n 1-w 5000% SERVER% ^ | find "% SERVER%" ^ | findstr / R "[0-9] \. [0-9] \ . [0-9] \. [0-9] "') do echo%% m

How to modify the code to print only the IP?

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Manipulate data

#2 Post by aGerman » 13 Aug 2013 12:37

Code: Select all

set "SERVER=www.google.com"
for /f "tokens=2 delims=[]" %%m in (
  'ping -n 1 %SERVER% ^| findstr "\[[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\]"'
) do echo %%m

Beware of misplaced spaces!

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Manipulate data

#3 Post by penpen » 13 Aug 2013 13:13

As the interesting line is the only one with [] chars, this should suffice:

Code: Select all

set "SERVER=www.google.com"
for /f "tokens=2 delims=[]" %%m in ('ping -n 1 %SERVER%') do echo %%m

penpen

Edit: corrected %m to %%m

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Manipulate data

#4 Post by alexandredneto » 14 Aug 2013 05:28

Thanks for your help.

rodrigolima
Posts: 21
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: Manipulate data

#5 Post by rodrigolima » 14 Aug 2013 08:50

Hello alexandredneto

I read your post and I did it.Check it:

I executed directly line command(I did not create batch file).

I divided in 2 Steps.

Step ONE:Get IP surronded by [IP]

Generate the file IP_RAW.txt

Set server=www.google.com.br

for /f "tokens=1-8 delims= " %i in ('ping -n 1 -w 5000 %server% ^| findstr /C:"["') do echo %k > IP_RAW.txt

Step TWO:Remove Only IP between [xxx.xxx.xxx.xxx]

for /f "tokens=1,2 delims=[,]" %i in (IP_RAW.txt) do echo %i > IP_EXTRACTED.txt

Check the file "IP_EXTRACTED.txt" and see ip address from server variable

in the end it works.

I hope that it help you.

See you and greetings from Brazil.

Post Reply