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 "^$"