Board logo

标题: [文本处理] 批处理怎么从文本文件中找指定的字符? [打印本页]

作者: hucuibai    时间: 2010-10-28 09:23     标题: 批处理怎么从文本文件中找指定的字符?

  1. @echo off
  2. set date=%date:~0,10%
  3. for /f "delims=: tokens=1,2" %%a in ('time /t') do set time=%%a-%%b
  4. set timedb=%date%---%time%
  5. echo %timedb%
  6. for /f %%i in (ip.txt) do (ping -n 100 %%i)>>%timedb%_result.txt
  7. pause
复制代码
我要从这个测试脚本得到的result.txt文件里面找出lost 和Average 两个值,应该怎么写!!高手赐教!!

[ 本帖最后由 hucuibai 于 2010-11-3 09:34 编辑 ]
作者: shihao    时间: 2010-10-28 10:20

  1. for /f "delims=, tokens=3,6" %a in (1.txt) do echo %a %b
复制代码

作者: hucuibai    时间: 2010-10-28 10:47     标题: 回复 2楼 的帖子

非常感谢,但是还是达不到要求,因为ip比较多,一个ip就有lost和Average 值,如果我想输出是每个ip后面是这个ip的lost和Average ,能不能这么做啊?
作者: hucuibai    时间: 2010-10-28 10:54     标题: 回复 2楼 的帖子

还有就是txt文件有很多,一个小时一次,用通配符貌似不行!
作者: leap    时间: 2010-10-28 13:16

  1. @echo off
  2. set date=%date:~0,10%
  3. for /f "delims=: tokens=1,2" %%a in ('time /t') do set time=%%a-%%b
  4. set timedb=%date%---%time%
  5. echo %timedb%
  6. for /f "tokens=4 delims==" %%j in ('type 1.txt^|findstr /i "lost average"') do (echo %%j>>%timedb%.txt)
  7. pause
复制代码

[ 本帖最后由 leap 于 2010-11-1 18:23 编辑 ]
作者: shihao    时间: 2010-10-29 09:28

原帖由 hucuibai 于 2010-10-28 10:47 发表
非常感谢,但是还是达不到要求,因为ip比较多,一个ip就有lost和Average 值,如果我想输出是每个ip后面是这个ip的lost和Average ,能不能这么做啊?

我认为是可以的。
首先,楼主题目是分析日志,不是写如何生成日志,不想了解楼主为什么要那样写,就楼主提供的日志样本,分析如下:
  1. echo off
  2. for /f "skip=3 delims=, tokens=1,3,6" %%a in (1.txt) do (for /f "tokens=2 delims=:" %%k in ("%%a") do echo %%k) & echo %%b %%c
  3. echo on
复制代码

楼主的生成方式我不想评价,就分析人员来说,我们只关心要分析的目标对象,而不会去关心这个对象是如何来的,因为我们只处理结果,结果产生的方法往往不是我们能涉及的。废话很多,主要想说的是不能同意楼上的方法。
另外,楼主你已经看到可以处理的,自己完全可以分析一下,毕竟您提供的日志还是比较有规律的。
我认为我提供的方法不是很好,但确实能工作,也能得到结果。
D:\temp>echo off
Reply from 202.101.115.55
Lost = 0 (0% loss)  Average = 75ms----2
Reply from 202.96.96.68
Lost = 0 (0% loss)  Average = 50ms----3
Reply from 202.101.224.68
Lost = 1 (1% loss)  Average = 55ms----4
Reply from 202.100.192.68
Lost = 0 (0% loss)  Average = 41ms----5
Reply from 202.98.96.68
Lost = 2 (2% loss)  Average = 154ms----6
Reply from 202.102.154.3
Lost = 0 (0% loss)  Average = 44ms----7
Reply from 202.98.192.68
Lost = 0 (0% loss)  Average = 100ms----8
Reply from 59.175.249.226
Lost = 0 (0% loss)  Average = 48ms2010-10-27 星期三
13
ECHO 处于关闭状态。

作者: hucuibai    时间: 2010-10-29 11:32     标题: 回复 6楼 的帖子

还是不行,我上传的那个日志文件打乱了,我再重新上传一个!!
作者: shihao    时间: 2010-10-29 16:26

我碰巧这几天比较空,我认为楼主应该自己分析一下,这个真的不难,要知道人家写东西,不管怎样,都要花时间和精力,近来看到的只是问这个咋整,那个咋整,难道分析下日志文本真的很难吗?我对批也不是很精通的,都是平日在实践中积累的。其实像批这类,花上两个星期基本就搞顶了,比什么c、java简单多了。
罗嗦了几句,请楼主包含,但确实有点有感而发。
就楼主最新的结果文件,大概写了下
  1. @echo off
  2. cd.>oo.txt
  3. for /f "delims=:" %%a in ('findstr /n "statistics" 1.txt') do echo %%a>>oo.txt
  4. for /f %%l in (oo.txt) do call :Readfi %%l 4 1.txt
  5. goto :eof
  6. :Readfi _fir _la _fi
  7. set /a s=%1-1
  8. SetLocal ENABLEDELAYEDEXPANSION
  9. set first="skip=%s% tokens=*"
  10. set last=%2
  11. set loop=0
  12. for /f %first% %%a in (%3) do (
  13. if not defined finished (echo %%a&set /a loop+=1) else (goto :end)
  14. if "!loop!"=="%last%" set finished=Nothing
  15. )
  16. :end
  17. endlocal
  18. @echo ===========================
  19. :eof
复制代码
得到如下结果,不知道达到要求了吗?
Ping statistics for 202.101.115.55:
Packets: Sent = 100, Received = 100, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 75ms, Maximum = 77ms, Average = 75ms
===========================
Ping statistics for 202.96.96.68:
Packets: Sent = 100, Received = 100, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 50ms, Maximum = 51ms, Average = 50ms
===========================
Ping statistics for 202.101.224.68:
Packets: Sent = 100, Received = 100, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 55ms, Maximum = 117ms, Average = 55ms
===========================
Ping statistics for 202.100.192.68:
Packets: Sent = 100, Received = 100, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 41ms, Maximum = 42ms, Average = 41ms
===========================
Ping statistics for 202.98.96.68:
Packets: Sent = 100, Received = 96, Lost = 4 (4% loss),
Approximate round trip times in milli-seconds:
Minimum = 66ms, Maximum = 154ms, Average = 71ms
===========================
Ping statistics for 202.102.154.3:
Packets: Sent = 100, Received = 100, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 44ms, Maximum = 80ms, Average = 44ms
===========================
Ping statistics for 202.98.192.68:
Packets: Sent = 100, Received = 100, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 100ms, Maximum = 104ms, Average = 100ms
===========================
Ping statistics for 59.175.249.226:
Packets: Sent = 100, Received = 97, Lost = 3 (3% loss),
Approximate round trip times in milli-seconds:
Minimum = 93ms, Maximum = 98ms, Average = 96ms
===========================

作者: hucuibai    时间: 2010-10-29 17:38     标题: 回复 8楼 的帖子

  1. @echo off
  2. cd.>oo.txt
  3. :input
  4. set /p file=请输入文件名
  5. for /f "delims=:" %%a in ('findstr /n "statistics" %file%.txt') do echo %%a>>oo.txt
  6. for /f %%l in (oo.txt) do call :Readfi %%l 4 %file%.txt
  7. goto :eof
  8. :Readfi _fir _la _fi
  9. set /a s=%1-1
  10. SetLocal ENABLEDELAYEDEXPANSION
  11. set first="skip=%s% tokens=*"
  12. set last=%2
  13. set loop=0
  14. for /f %first% %%a in (%3) do (
  15. if not defined finished (echo %%a&set /a loop+=1) else (goto :end)
  16. if "!loop!"=="%last%" set finished=Nothing
  17. )
  18. :end
  19. endlocal
  20. @echo ===========================
  21. :goto :eof
  22. pause
复制代码
我今天早上来研究了三个小时啊,刚开始学批处理,不太懂,对你给的这个代码有些我都看不懂,我刚刚看了一下你给的,这个也只能解决一个文件啊,我的这个日志文件很多,换成通配符好像不行哦,然后我想用手动输入的,但是执行完之后要怎么回到输入的地方啊,刚刚也研究了半天,还是搞不出来,肚子里面没货啊,搞不成!!呵呵!!
作者: shihao    时间: 2010-11-1 09:01

对不起,楼主,我实在没法回答了,我要忙一段时间了。就日志文件很多的问题,你可以做个循环阿,把现在的东西写好,直接call就行了。
取文件的循环大概可以这样写
  1. for /r %%a in (*.txt) do call :Process %%a
复制代码
就此别过
作者: qq106942397    时间: 2010-11-1 11:40

把楼上修改吓,楼上的结果正确不正确我也对过,只修改把P文件目录里的所有TXT操作
@echo off




:input
for /f "delims=*" %%z in ('dir /a *.txt') do (cd.>oo.txt
for /f "delims=:" %%a in ('findstr /n "statistics" "%%~z"') do echo %%a>>oo.txt
for /f %%l in (oo.txt) do call :Readfi %%l 4 "%%~z")
pause
goto :eof

:Readfi _fir _la _fi
set /a s=%1-1
SetLocal ENABLEDELAYEDEXPANSION
set first="skip=%s% tokens=*"
set last=%2
set loop=0
for /f %first% %%a in (%3) do (
if not defined finished (echo %%a&set /a loop+=1) else (goto :end)
if "!loop!"=="%last%" set finished=Nothing
)
:end
endlocal
@echo ===========================
:goto :eof



pause
作者: hucuibai    时间: 2010-11-1 17:28     标题: 回复 11楼 的帖子

还是没搞通!!!!!!!
作者: cjiabing    时间: 2010-11-2 19:02

首先,问你这个文件是用什么命令输出的?干嘛都堆到一行来了?
其次,你用这个文件来做什么?
作者: hucuibai    时间: 2010-11-3 09:16     标题: 回复 13楼 的帖子

你是下了我最前面一个文件吧,那个是错的,正确的输出是我7楼的那个,这个文件是一个批处理输出的!!现在我就是想得到lost和
Average 这两个的值!
作者: Batcher    时间: 2010-11-3 09:28     标题: 回复 14楼 的帖子

请把最新的文档更新到顶楼
作者: gshccr    时间: 2010-11-4 09:32

不知道是不是这样!
  1. @echo off
  2. for /f "tokens=3 delims=," %%i in ('findstr /i "Average lost" "1.txt"'
  3.   ) do (echo %%i>>已完成.txt)
  4. pause
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2