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-07-02 14:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question: how to search for a specific string in a txt file View 5,034 Replies 20
Original Poster Posted 2005-11-09 15:03 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
If in the DOS under xp/2003/2000 there is a text file as.txt whose content is ( Pinging www.cctv.chinacache.net with 32 bytes of data, how can I extract the IP part after , note that the IP is variable, and then append this IP with >> to another txt, thanks!
Floor 2 Posted 2005-11-09 16:17 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
I don't have xp
If it were me under DOS, I would cut the text apart at ''. For example:
Pinging www.cctv.chinacache.net

with 32 bytes of data

Then just extract the line containing ']'
Floor 3 Posted 2005-11-09 17:40 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re malongxa:

Try running the following command at the command line first. If it works, tell me your exact requirements, and then I can try to give a more complete and robust solution.

for /f "delims=[] tokens=2" %i in (as.txt) do echo.%i>>Result.txt
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2005-11-10 09:26 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
Moderator, you're amazing. Using your method, for /f "delims=[] tokens=2" %i in (as.txt) do echo.%i>>temp, I got the result I wanted, and I learned something from it too. Thank you, thanks again!!!
Floor 5 Posted 2005-11-10 10:38 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
Moderator, what I want is first to ping xxx.com, then find the IP (yyy.yyy.yyy.yyy) in the result, and then use that as the execution parameter for another ping command, like ping yyy.yyy.yyy.yyy. Because I use a dynamic IP, every time the IP changes it becomes troublesome. I want to learn to write batch files myself, but I still don't understand many parameters. I hope I can learn more from you, moderator, thank you!!
Floor 6 Posted 2005-11-10 15:25 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
The batch file I wrote, cctv.bat
ping www.cctv.com >as.txt
for /f "delims= token=2" %i in (as.txt) do ping %i
Each line works when executed separately, but when put together it doesn't work. Please advise, thanks!
Floor 7 Posted 2005-11-10 19:51 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re malongxa:

When using a for statement in a batch file, the replacement variable must be preceded by a double percent sign, that is, %i should be changed to %%i. This comes from the difference in how code lines are parsed in batch files and at the command line.

Also, these two lines of code can be tried in one line. I'm still giving the command-line version:

for /f "delims= token=2" %i in ('ping www.cctv.com') do ping %i
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2005-11-10 22:22 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
Unfortunately, under dos/98 you can't use that many for parameters~
Floor 9 Posted 2005-11-11 10:18 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
Thanks for the moderator's help. I also want to further improve my idea. Suppose I now have a text file as.txt

Alternate TCP addresses for

Local Address Alternate Address
-------------------- --------------------
Default 219.137.124.26
I want to extract the IP in the text, namely (219.137.124.26)
Compare this IP with the IP obtained from ping www.cctv.com
If the IPs are the same, do nothing
If the IPs are different, then execute
for /f "delims= token=2" %i in ('ping www.cctv.com') do ping %i,
Thanks again for the moderator's guidance

[ Last edited by malongxa on 2005-11-11 at 14:28 ]
Floor 10 Posted 2005-11-11 14:34 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
I can already extract the IP from AS.TXT
Using for /f "delims=ault tokens=2" %i in (as.txt) do echo %i
But the extracted IP has spaces before it
What I don't know now is how to compare and judge the difference between this IP and the IP extracted from ping www.cctv.com
Please advise, thanks!
Floor 11 Posted 2005-11-12 00:55 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline

Using for /f "delims=ault tokens=2" %i in (as.txt) do echo %i
But the extracted IP has spaces before it

Change the order, like this:
for /f "tokens=2 delims= " %i in (as.txt) do echo %i

I really don't know what you ultimately want to do. Below is the function you wanted in your post #9.
Save it as a batch file and execute it.

for /f "tokens=2 delims=" %%i in ('ping www.cctv.com') do set a=%%i
for /f "tokens=2 delims= " %%i in (as.txt) do set b=%%i
if "%a%" NEQ "%b%" for /f "tokens=2 delims=" %%i in ('ping www.cctv.com') do ping %%i

Floor 12 Posted 2005-11-13 00:28 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
Thanks, 无奈何. I tested it as you instructed and it worked. If I still want to keep using a and b as the condition and continue executing commands, then can I add this after your batch file:
if "%a%" NEQ "%b%" ping www.cctv.com
Or should I directly add ping www.cctv.com after if "%a%" NEQ "%b%" for /f "tokens=2 delims=" %%i in ('ping www.cctv.com') do ping %%i
Writing it as if "%a%" NEQ "%b%" for /f "tokens=2 delims=" %%i in ('ping www.cctv.com') do ping %%i ping www.cctv.com
Thanks again!
Floor 13 Posted 2005-11-13 00:43 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
After if ... just put the statement you want to execute directly.
If there are multiple statements, you can connect them with &
For example:
if "%a%" NEQ "%b%" dir
if "%a%" NEQ "%b%" dir & echo OK
Floor 14 Posted 2005-11-14 11:07 ·  中国 陕西 西安 电信
新手上路
Credits 16
Posts 6
Joined 2005-11-14 10:07
20-year member
UID 45208
Status Offline
If I want to extract any value from a text file, what should I do? Thanks!!!
For example, there is a text file a.txt with the following content:
Local Address Alternate Address
I want to extract “nate” from it, what should I do
Floor 15 Posted 2005-11-14 22:10 ·  中国 陕西 西安 电信
初级用户
Credits 26
Posts 11
Joined 2005-11-09 14:40
20-year member
UID 44898
Status Offline
Could the moderator and 无奈何, you two experts, explain in detail how to use "delims=[] token=2"? I still have no way to capture specific characters. Thanks again to both of you experts!!
Forum Jump: