标题: [文本处理] 求助批处理如何提取ping平均值? [打印本页]
作者: laotou1000 时间: 2009-10-9 11:14 标题: 求助批处理如何提取ping平均值?
我编写了一个提取拼平均值的批处理代码,如下:- @echo off
- for /l %%p in (130,1,150) do @ping 172.16.3.%%p -n 1 -w 10 | find " " /i >>旧.txt
- (for /f "tokens=1,2,6 delims== " %%i in ('findstr /i "Pinging Average" 旧.txt') do if /i "%%i" == "Pinging" (set /p =%%j:<nul) else set /p =Average=%%k<nul&echo.)>>新.txt
- pause
复制代码
运行结果如下:- 旧.txt
-
- Pinging 172.16.3.130 with 32 bytes of data:
-
- Reply from 172.16.3.130: bytes=32 time=58ms TTL=64
-
- Ping statistics for 172.16.3.130:
-
- Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
-
- Approximate round trip times in milli-seconds:
-
- Minimum = 58ms, Maximum = 58ms, Average = 58ms
-
- Pinging 172.16.3.131 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.131:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.132 with 32 bytes of data:
-
- Reply from 172.16.3.132: bytes=32 time<1ms TTL=128
-
- Ping statistics for 172.16.3.132:
-
- Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
-
- Approximate round trip times in milli-seconds:
-
- Minimum = 0ms, Maximum = 0ms, Average = 0ms
-
- Pinging 172.16.3.133 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.133:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.134 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.134:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.135 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.135:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.136 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.136:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.137 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.137:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.138 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.138:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.139 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.139:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.140 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.140:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.141 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.141:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.142 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.142:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.143 with 32 bytes of data:
-
- Reply from 172.16.3.143: bytes=32 time=1ms TTL=128
-
- Ping statistics for 172.16.3.143:
-
- Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
-
- Approximate round trip times in milli-seconds:
-
- Minimum = 1ms, Maximum = 1ms, Average = 1ms
-
- Pinging 172.16.3.144 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.144:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.145 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.145:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.146 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.146:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.147 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.147:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.148 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.148:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.149 with 32 bytes of data:
-
- Request timed out.
-
- Ping statistics for 172.16.3.149:
-
- Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
-
- Pinging 172.16.3.150 with 32 bytes of data:
-
- Reply from 172.16.3.150: bytes=32 time=1ms TTL=128
-
- Ping statistics for 172.16.3.150:
-
- Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
-
- Approximate round trip times in milli-seconds:
-
- Minimum = 1ms, Maximum = 1ms, Average = 1ms
复制代码
- 新.txt
-
- 172.16.3.130:Average=58ms
-
- 172.16.3.131:172.16.3.132:Average=0ms
-
- 172.16.3.133:172.16.3.134:172.16.3.135:172.16.3.136:172.16.3.137:172.16.3.138:172.16.3.139:172.16.3.140:172.16.3.141:172.16.3.142:172.16.3.143:Average=1ms
-
- 172.16.3.144:172.16.3.145:172.16.3.146:172.16.3.147:172.16.3.148:172.16.3.149:172.16.3.150:Average=1ms
复制代码
现在我想要将 新.txt 中的显示内容修改为- 172.16.3.130:Average=58ms
- 172.16.3.131:
- 172.16.3.132:Average=0ms
- 172.16.3.133:
- 172.16.3.134:
- 172.16.3.135:
- 172.16.3.136:
- 172.16.3.137:
- 172.16.3.138:
- 172.16.3.139:
- 172.16.3.140:
- 172.16.3.141:
- 172.16.3.142:
- 172.16.3.143:Average=1ms
- 172.16.3.144:
- 172.16.3.145:
- 172.16.3.146:
- 172.16.3.147:
- 172.16.3.148:
- 172.16.3.149:
- 172.16.3.150:Average=1ms
复制代码
即每个IP后面跟一个平均值,而且一个IP一行,我应该如何修改代码?请各位前辈指教。
[ 本帖最后由 laotou1000 于 2009-10-9 11:16 编辑 ]
作者: hlzj88 时间: 2009-10-9 23:22
看这个贴挂了12小时,没人发言,我就说一下我的理解,因为写不出
set /p =%%j:<nul 可能是这句导致后面新.txt数据连续
Average = 1ms 如果要取这句,可以 skip=n 掉前面的几行 用delims或findstr来截取到Average = 1ms(也可以截取TTL) 再set a后得到 1(先去掉Average = )(再倒过来截去掉ms) ,再set b先等于0,令set b=(set a)+b(这里的b是多个a相加),再除以(150-130)就是你要的平均值,貌似不用写入文本,关键在截取到数字
不会说,希望我的思路你能理解。
作者: laotou1000 时间: 2009-10-10 09:21 标题: 回复 2楼 的帖子
感谢hlzj88的热心帮助!!!
不过在“再set b先等于0,令set b=(set a)+b(这里的b是多个a相加),再除以(150-130)就是你要的平均值”中,为什么要让多个a相加,再除以(150-130)呢?
作者: hlzj88 时间: 2009-10-10 20:32
a 是每次ping得到的数,b是每次a的值相加的和,(150-130)是你ping的总次数。
刚在线翻译了一下。Average为平均,哈哈,英语不行,就是不行。各位见笑了。
[ 本帖最后由 hlzj88 于 2009-10-10 20:38 编辑 ]
作者: laotou1000 时间: 2009-10-12 08:53 标题: 回复 4楼 的帖子
您好像弄错了吧?我要的不是您说的这个,我是想要从ping的结果中提取其中的average。
作者: duanlihong 时间: 2009-11-23 14:35 标题: 请参考
- @echo off&setlocal enabledelayedexpansion
- echo *****************************监测启动****************************************
- cd c:\
- for /f "tokens=1* delims=:" %%a in (a.txt) do (
- for /f "tokens=3 delims=," %%i in ('ping -l 1000 -n 2 %%a ^|findstr Average') do echo %%a %%i>>ping.txt
- )
- pause
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |