中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-25 23:44
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » Question: how to search for a specific string in a txt file
Printable Version  5,379 / 20
Floor1 malongxa Posted 2005-11-09 15:03
初级用户 Posts 11 Credits 26
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!
Floor2 bush Posted 2005-11-09 16:17
银牌会员 Posts 730 Credits 2,165
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 ']'
Floor3 willsort Posted 2005-11-09 17:40
元老会员 Posts 1,512 Credits 4,432
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
Floor4 malongxa Posted 2005-11-10 09:26
初级用户 Posts 11 Credits 26
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!!!
Floor5 malongxa Posted 2005-11-10 10:38
初级用户 Posts 11 Credits 26
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!!
Floor6 malongxa Posted 2005-11-10 15:25
初级用户 Posts 11 Credits 26
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!
Floor7 willsort Posted 2005-11-10 19:51
元老会员 Posts 1,512 Credits 4,432
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
Floor8 bush Posted 2005-11-10 22:22
银牌会员 Posts 730 Credits 2,165
Unfortunately, under dos/98 you can't use that many for parameters~
Floor9 malongxa Posted 2005-11-11 10:18
初级用户 Posts 11 Credits 26
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 ]
Floor10 malongxa Posted 2005-11-11 14:34
初级用户 Posts 11 Credits 26
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!
Floor11 无奈何 Posted 2005-11-12 00:55
荣誉版主 Posts 356 Credits 1,338

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.
Floor12 malongxa Posted 2005-11-13 00:28
初级用户 Posts 11 Credits 26
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!
Floor13 无奈何 Posted 2005-11-13 00:43
荣誉版主 Posts 356 Credits 1,338
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
Floor14 zzhynxxn Posted 2005-11-14 11:07
新手上路 Posts 6 Credits 16
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
Floor15 malongxa Posted 2005-11-14 22:10
初级用户 Posts 11 Credits 26
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!!
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023