我想在批處理腳本中捕獲 iPerf 結果。最后幾行輸出就像
[ 23] 0.00-15.22 sec 107 KBytes 57.8 Kbits/sec 32 sender
[ 23] 0.00-15.00 sec 63.7 KBytes 34.8 Kbits/sec receiver
[SUM] 0.00-15.22 sec 100 MBytes 55.4 Mbits/sec 711 sender
[SUM] 0.00-15.00 sec 92.9 MBytes 52.0 Mbits/sec receiver
iperf Done.
我需要找到包含關鍵字[SUM]和receiver. 我目前的解決方案是,我知道它必須出現在倒數第二行,所以我直接搜索該行。代碼在下面。
@echo off
setlocal enableextensions enabledelayedexpansion
iperf3 -c iperf.scottlinux.com -i 1 -t 15 -P 10 -p 5201 -R>>rawdata.txt 2>&1
call :FilterResultLine rawdata.txt 2 resultLineContent
for /F "tokens=6,7" %%a in ("%resultLineContent%") do (
echo Bitrate [DL]: %%a %%b>>result.txt
)
pause
exit
:FilterResultLine %rawdata% %resultLineNumber% %resultLineContent%
set /A firstTail=1, lastTail=0
for /F "delims=" %%a in (%1) do (
set /A lastTail =1, lines=lastTail-firstTail 1
set "lastLine[!lastTail!]=%%a"
if !lines! gtr %~2 (
set "lastLine[!firstTail!]="
set /A firstTail =1
)
)
set "%~3=!lastLine[%firstTail%]!"
goto :eof
但是,如果輸出格式發生變化,這可能會不穩定。我想知道批處理腳本是否可以在每一行中搜索關鍵字?
uj5u.com熱心網友回復:
你可以試試這個例子:
@echo off
Title Filter and find a line with findstr
REM In this example the file rawdata.txt is in the same folder with this batch file
Set "InputFile=%~dp0rawdata.txt"
If Exist "%InputFile%" (
Goto Main
) Else (
echo( & Color 0C
echo( Check the location of this file "%InputFile%" & Timeout /T 5 /NoBreak>nul & Exit
)
::-----------------------------------------------------------------------------------------
:Main
echo( ---------------------------------------------------------------------------------
echo( Showing All lines without filtring
echo( ---------------------------------------------------------------------------------
@for /f "delims=" %%a in ('Type "%InputFile%"') do (echo %%a)
pause
echo( ---------------------------------------------------------------------------------
echo Find only lines with only "[SUM]" in their contents
echo( ---------------------------------------------------------------------------------
@for /f "delims=" %%a in ('Type "%InputFile%" ^| findstr /I "\[SUM\]"') do (echo %%a)
pause
echo( ---------------------------------------------------------------------------------
echo Find only lines with "[SUM]" and "receiver" in their contents
echo( ---------------------------------------------------------------------------------
@for /f "delims=" %%a in (
'Type "%InputFile%" ^| findstr /I "\[SUM\]" ^| findstr /I "receiver"'
) do (echo %%a)
echo( ---------------------------------------------------------------------------------
pause
Exit /B
::-----------------------------------------------------------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312629.html
上一篇:洗掉Loop最后一個值上的逗號
下一篇:為什么子字串不適用于命令列引數
