bat如何提取文本指定行的內容


背景:使用CTS框架運行完測試后,會在logs中生成devices_log和host_log,在results中生成相應的結果(報告)。根據報告信息我們可以得知失敗的用例,但是卻不能知道為什么用例會失敗,是腳本有問題?設備有問題?還是其他.....。此時我們就得通過 截圖、視頻、log等信息進行分析。然而事實卻是很痛苦的,打開log一看密密麻麻的,整個module的日志都在這里面。我就看失敗的那條日志,難道還要我選中一段,然后Ctrl+C、Ctrl+V嗎?那條用例的log有上千行啊!mmp(通過關鍵字查找雖然能找到失敗的那條,想再進行過濾的話,總是會被其他日志干擾)

問題:我怎樣才能拿到我想要的那段log呢?

思路:從log看到會有“TestRunner:started:” 和“TestRunner: finished:”字符串,也就是用例的開始和結束。只要拿到開始和結束的行數就行了。

解決:輸入行數提取文本內容。

No1:輸入需要截取文本內容的開始和結束行即可。

@echo off &setlocal enabledelayedexpansion
title ExtractContent
color 0a
rem ++++++++++++++++++++++++++++++++++++++++
rem   Extract.bat
rem      By zhzw @2018/1/18
rem   
rem         Version: 1.0
rem ++++++++++++++++++++++++++++++++++++++++
echo 1.Can't handle empty lines 
:next
echo.
echo 請輸入開始行和結束行^>^>^>
set /p sRow=StartRow:
set /p eRow=EndRow:

set stime=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
if "%stime:~0,1%"==" " set "stime=0%stime:~1%"
set /a sRow=%sRow%-1 
if "%sRow%"=="0" (set sRow=) else set sRow=skip=%sRow%

set start=%sRow:~5%
set stop=%eRow%
if "%stop%"=="" echo."EndRow" cant be equal to null &goto done

for /f "%sRow% tokens=* delims=" %%a in (device_logcat.txt) do (
    rem echo %sRow%
    set /a start+=1
    echo %stop%----!start!
    echo.%%a >>%stime%.txt
    if "%stop%"=="!start!" goto end
)
:end
echo done
set etime=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
if "%etime:~0,1%"==" " set "etime=0%time:~1%"
echo 開始時間:%stime%
echo 結束時間:%etime%
rem timeout /t 2 &exit
:done
ping 127.0.0.1 -n 3 >nul 2>nul &echo.
echo 終止提取Ctrl+C;繼續提取,請按任意鍵繼續...&pause >nul
cls &echo.Try again extract...&goto next

 上面的方法不能處理空行(默認不會把空行算為一行),如果文本中出現空行則會導致與所需要的文本內容行數有相差。速度快(貌似是個完美主義者!~_~)

No2:處理了空行的問題。如果行數過大且文本超過MB,讀取速度慢,而且行數相差大。(讀取整個文本>忽略開始行數-1>截取分隔>寫入文本)

@echo off
title ExtractContent
color 0a
rem @mode con lines=35 cols=75
rem ++++++++++++++++++++++++++++++++++++++++
rem   Extract.bat
rem      By zhzw @2018/1/18
rem   
rem         Version: 1.0.1
rem ++++++++++++++++++++++++++++++++++++++++
echo 1."StartRow" and "EndRow" cant be equal to null
echo.
set /p sRow=StartRow:
set /p eRow=EndRow:

set stime=%time:~0,2%%time:~3,2%%time:~6,2%
if "%stime:~0,1%"==" " set "stime=0%stime:~1%"
set /a sRow=%sRow%-1 
if "%sRow%"=="0" (set sRow=) else set sRow=skip=%sRow%


for /f "%sRow% tokens=1,* delims=:" %%a in ('findstr /n ".*" device_logcat.txt') do (
    rem echo %sRow%
    echo %eRow%----%%a
    echo.%%b >>%stime%.txt
    if %eRow%==%%a goto end
)
:end
echo.
echo done
set etime=%time:~0,2%%time:~3,2%%time:~6,2%
if "%etime:~0,1%"==" " set "etime=0%time:~1%"
echo 開始時間:%stime%
echo 結束時間:%etime%
rem timeout /t 2 &exit
pause

No3:彌補數據准確性的問題,大行數時速度慢[No1>No3>No2]。(以行號查找文本中指定行>寫入文本)

@echo off &setlocal enabledelayedexpansion
title ExtractContent
color 0a
rem ++++++++++++++++++++++++++++++++++++++++
rem   Extract.bat
rem      By zhzw @2018/1/18
rem   
rem         Version: 1.0.2
rem ++++++++++++++++++++++++++++++++++++++++
echo 1."StartRow" and "EndRow" cant be equal to null
echo.
set /p sRow=StartRow:
set /p eRow=EndRow:

set stime=%time:~0,2%%time:~3,2%%time:~6,2%
if "%stime:~0,1%"==" " set "stime=0%stime:~1%"

set start=%sRow%
set /a stop=%eRow%+1
echo.
:replay
rem echo %start%
for /f "tokens=1,* delims=:" %%a in ('findstr /n ".*" device_logcat.txt ^| findstr /b "\<%start%:"') do (
    set /a start+=1
    echo %stop%----!start!
    if "%stop%"=="!start!" goto end
    echo.%%b >>%stime%.txt &goto replay
)
:end
echo.&echo done
set etime=%time:~0,2%%time:~3,2%%time:~6,2%
if "%etime:~0,1%"==" " set "etime=0%etime:~1%"
echo 開始時間:%stime%
echo 結束時間:%etime%
rem timeout /t 2 &exit
pause

 

去除空行處理:

type a.txt | findstr /v "^$" 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM