China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 00:18
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question about getting a specified position in text View 725 Replies 7
Original Poster Posted 2008-05-30 17:44 ·  中国 广东 广州 移动
初级用户
Credits 70
Posts 36
Joined 2008-05-21 22:48
18-year member
UID 119349
Gender Male
Status Offline
I'm new here, and I'm not familiar with some problems and commands yet. I hope the experts can give me some pointers.. thanks

I need to find out my own IP, then based on that IP, find the corresponding server address from a data text file.
And it has to be set automatically....

Here is my line of thought, and the part I can't figure out:

There is a data address for an IP stored in a document.. the format is as follows:
10.248.52.63 10.248.31.15 ......
10.248.52.64 10.248.31.16 ......
10.248.52.65 10.248.31.16 ......
10.248.52.166 10.248.31.16 ......
10.248.52.67 10.248.31.32 ......
10.248.52.168 10.248.31.32 ......
10.248.52.69 10.248.31.32 ......
10.248.52.170 10.248.31.33 ......

Then get my own IP and save it into the myip.txt document
for /f "tokens=15" %%i in ('ipconfig ^| find /i "ip address"' do set ip=%%i
echo Your IP is:%ip%
( find "%ip%" < icdconfig.txt ) >myip.txt ::

After that there is one line of data in myip.txt, such as:
10.248.52.63 10.248.31.15 ......

These data were converted from EXCLE. The ".........." at the back are the remaining several server IP addresses... the two IP addresses are separated by a Tab.
What I need is to get the last segment of 10.248.31.15 in this IP (the number 15)
and store it in a variable for later use

The problem is here: how can I get the number 15 and store it into a variable
for me to call again?
I tried using for and findstr, but neither can specify this position.......

Please help me.....

[ Last edited by andison625 on 2008-6-1 at 09:52 AM ]
Floor 2 Posted 2008-05-30 18:12 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
After reading the OP's description, I cried. I've studied Chinese for so many years, yet I still can't understand what the OP is trying to say. Looks like I need to go study Chinese for a few more years.
批处理之家新域名:www.bathome.net
Floor 3 Posted 2008-05-30 18:19 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-5-30 18:12:
After reading the OP's description, I cried. I've studied Chinese for so many years, yet I still can't understand what the OP is trying to say. Looks like I need to go study Chinese for a few more years.

Hahaha, I thought I was the only one who couldn't understand it because I haven't read many books, but turns out you can't understand it either。。。。!
致精致简!
Floor 4 Posted 2008-05-30 18:34 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
What the OP seems to mean is:
for /f "tokens=2" %%a in (myip.txt) do @(for /f "delims=. tokens=4" %%b in ("%%a") do @echo.%%b)
Floor 5 Posted 2008-06-01 09:40 ·  中国 广东 广州 移动
初级用户
Credits 70
Posts 36
Joined 2008-05-21 22:48
18-year member
UID 119349
Gender Male
Status Offline
What I mean is... get a specified value from text.
192.168.0.12 10.248.31.15

What I want is the last segment of the second IP address (the number 15), and assign it to a variable for me to use..
Floor 6 Posted 2008-06-01 09:49 ·  中国 广东 广州 移动
初级用户
Credits 70
Posts 36
Joined 2008-05-21 22:48
18-year member
UID 119349
Gender Male
Status Offline
Thanks, moderator lxmxn...
for /f "tokens=2" %%a in (myip.txt) do @(for /f "delims=. tokens=4" %%b in ("%%a") do @echo.%%b)

This method found the position I wanted.

But I still don't understand the usage of for very well, it seems quite different from the languages I've learned
Does the forum have any batch programming tutorial like CHM available for download?
I really want to learn batch programming well......
Floor 7 Posted 2008-06-01 10:02 ·  中国 江西 九江 电信
初级用户
Credits 51
Posts 25
Joined 2008-04-27 10:13
18-year member
UID 117080
Gender Male
From 学校
Status Offline
Not sure why, but after reading the OP's description, my head feels a bit dizzy! What exactly is it?
Floor 8 Posted 2008-06-01 19:05 ·  中国 广西 南宁 电信
中级用户
★★
丶杏灬丶
Credits 280
Posts 105
Joined 2008-05-06 03:37
18-year member
UID 117858
Gender Male
From 广西
Status Offline
@echo off&&setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1*" %%i in (myip.txt) do (
if "%%i"=="192.168.0.12" (
echo corresponding server IP: %%j&&set ips=%%j
for /f "delims=. tokens=1-4 " %%a in ("!ips!") do (
set ipxs=%%d&echo last part of the corresponding server IP: !ipxs!
)))




Is this what you want to achieve?
Replace the variables yourself..... not sure whether this is what you mean


[ Last edited by joyn on 2008-6-1 at 07:10 PM ]
Forum Jump: