Home > Software engineering >  Batch script for scanning each line of keywords in the command output
Batch script for scanning each line of keywords in the command output

Time:10-07

I want to capture the iPerf result in a batch script. The last few lines of output are like

[ 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.

I need to find the line that includes keywords [SUM] and receiver. My current solution is, I know it must appear in the second-to-last line, so I directly search for that line. The code is at below.

@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

However, if the output format changes, this may be unstable. I want to know whether the batch script can search for keywords in each line?

CodePudding user response:

You can try out this example :

@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
::-----------------------------------------------------------------------------------------
  • Related