我编写了一个批处理脚本,用于从文本文件中捕获特定位置字符串,例如:
文本文件包含:
Copyright Version 5.39
Network activity progressing...
Thread Time(s) Throughput(KB/s) Avg B / Compl
====== ======= ================ =============
0 600.088 73245.829 45502.632
1 594.574 84312.667 44974.835
2 594.569 62862.184 44547.486
3 599.665 66407.148 45056.270
4 600.633 61846.742 44741.495
5 594.569 45967.918 46745.891
6 594.937 72678.901 45115.861
7 593.981 86081.374 45148.288
8 593.975 35448.661 44118.093
9 602.451 64144.439 44708.118
10 599.760 26404.342 53411.916
11 594.569 64959.044 44525.327
12 594.564 63125.969 44512.966
13 602.999 71045.335 45266.114
14 599.719 19782.849 54192.569
15 594.574 61670.399 44341.198
16 599.681 71804.247 44954.492
17 593.980 21731.533 43903.776
18 593.979 22436.796 43919.327
19 599.748 21296.983 53880.446
##### Totals: #####
Bytes(MEG) realtime(s) Avg Frame Size Throughput(MB/s)
================ =========== ============== ================
639903.106318 600.002 4289.297 1066.502
Throughput(Buffers/s) Cycles/Byte Buffers
===================== =========== =============
17064.032 41.043 10238449.701
DPCs(count/s) Pkts(num/DPC) Intr(count/s) Pkts(num/intr)
============= ============= =============== ==============
3081479.450 0.085 2302796.649 0.113
Packets Sent Packets Received Retransmits Errors Avg. CPU %
============ ================ =========== ====== ==========
66199455 156432871 4 0 99.998
文本文件中共有 58 行,我需要获取在第 43 行分配的“吞吐量(MB/s)”值,以及第四个字符串,在上面的示例中它是“1066.502”。
因此,我创建了一个批处理脚本来过滤掉数字:
set "lom1="
for /f "skip=48 delims=" %%i in LOM_1.log) do if not defined lom1 set "lom1=%%i"
echo !lom1! >> temp1.txt
for /f "tokens=4" %%j in temp1.txt do echo %%j >> result.log
但是,现在源文本出现错误,并且增加了随机行,例如:
Copyright Version 5.39
Network activity progressing...
ERROR: WaitForWorkerThreads failed: WaitForMultipleObjects returned an unexpected value
ERROR: DoWork failed: WaitForWorkerThreads(threads_finished) timed out
ERROR: StartSenderReceiver in thread: 9 failed: closesocket, GetLastError: 10093 - Either the application has not called WSAStartup, or WSAStartup failed.
ERROR: StartSenderReceiver in thread: 13 failed: closesocket, GetLastError: 10093 - Either the application has not called WSAStartup, or WSAStartup failed.
ERROR: StartSenderReceiver in thread: 16 failed: closesocket, GetLastError: 10093 - Either the application has not called WSAStartup, or WSAStartup failed.
Thread Time(s) Throughput(KB/s) Avg B / Compl
====== ======= ================ =============
0 600.088 73245.829 45502.632
1 594.574 84312.667 44974.835
2 594.569 62862.184 44547.486
3 599.665 66407.148 45056.270
4 600.633 61846.742 44741.495
5 594.569 45967.918 46745.891
6 594.937 72678.901 45115.861
7 593.981 86081.374 45148.288
8 593.975 35448.661 44118.093
9 602.451 64144.439 44708.118
10 599.760 26404.342 53411.916
11 594.569 64959.044 44525.327
12 594.564 63125.969 44512.966
13 602.999 71045.335 45266.114
14 599.719 19782.849 54192.569
15 594.574 61670.399 44341.198
16 599.681 71804.247 44954.492
17 593.980 21731.533 43903.776
18 593.979 22436.796 43919.327
19 599.748 21296.983 53880.446
##### Totals: #####
Bytes(MEG) realtime(s) Avg Frame Size Throughput(MB/s)
================ =========== ============== ================
639903.106318 600.002 4289.297 1066.502
Throughput(Buffers/s) Cycles/Byte Buffers
===================== =========== =============
17064.032 41.043 10238449.701
DPCs(count/s) Pkts(num/DPC) Intr(count/s) Pkts(num/intr)
============= ============= =============== ==============
3081479.450 0.085 2302796.649 0.113
Packets Sent Packets Received Retransmits Errors Avg. CPU %
============ ================ =========== ====== ==========
66199455 156432871 4 0 99.998
您可以看到文本文件中还有额外的“ERROR”行,这意味着我仍然无法使用“for /f”skip=48”命令来过滤掉正确的行,所以,我想知道,是否可以使用任何批处理命令或“for /f”参数从最后一行开始计数,以便始终固定行号,例如在上面的文本内容上,如果我从最后一行开始计数,第 16 行是什么我想要,然后我可以继续导出第 16 行(从最后一行算起),然后捕获该数字的第 4 个字符串。
提前致谢。
我尝试用谷歌搜索,但找不到有用的信息。
以下是我如何尝试实现从每个日志文件中检索这些吞吐量值的任务的一个示例:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
PushD "P:\ath To\Log Directory" 2>NUL || Exit /B
(CD.) 1>"%~dp0result.log"
For /F "Tokens=1-2 Delims=:" %%G In ('%SystemRoot%\System32\findstr.exe
/INR "Throughput(MB/s)" "lom_*.log" 2^>NUL') Do Call :Sub "%%G" "%%H"
Pause
PopD
EndLocal
GoTo :EOF
:Sub
Set /A "_l=%2 + 1"
SetLocal EnableDelayedExpansion
(For /F "Tokens=4" %%I In ('%SystemRoot%\System32\more.com +!_l! "%1"'
) Do EndLocal & Set /P "=%%I" 0<NUL & Echo(& GoTo :EOF) 1>>"%~dp0result.log"
您显然应该调整第四行的日志文件路径,并注意我假设日志文件名的模式是
lom_*.log
。生成的文件将驻留在与批处理文件相同的目录中。