我正在尝试删除.txt文件中不需要的文本而不是整行。 以下是文字示例:
C:\Users\cph\AppData\Local\Temp\results_2017-01202016.txt: Geekbench Score 3646 7510
C:\Users\cph\AppData\Local\Temp\results_2017-01202016.txt:Rendering (Multiple CPU) : 342.39 pts
C:\Users\cph\AppData\Local\Temp\results_2017-01202016.txt:Shading (OpenGL) : 58.11 fps
C:\Users\cph\AppData\Local\Temp\results_2035-01202016.txt: Geekbench Score 3654 7511
C:\Users\cph\AppData\Local\Temp\results_2035-01202016.txt:Rendering (Multiple CPU) : 340.95 pts
C:\Users\cph\AppData\Local\Temp\results_2035-01202016.txt:Shading (OpenGL) : 57.47 fps
C:\Users\cph\AppData\Local\Temp\results_2052-01202016.txt: Geekbench Score 3657 7524
C:\Users\cph\AppData\Local\Temp\results_2052-01202016.txt:Rendering (Multiple CPU) : 341.38 pts
C:\Users\cph\AppData\Local\Temp\results_2052-01202016.txt:Shading (OpenGL) : 57.79 fps
我只需要保留FPS,pts和Score部分(数字),但这就是文本文件中的内容。如果我必须使用findstr拆分它,然后剥离对我来说没问题的东西。
如果您能想到任何事情,请告诉我。我知道如何在Python中完成它但我们不允许为此安装任何额外的工具。
输出需要这样。
Geekbench 3646 7510
Geekbench 3654 7511
Geekbench 3657 7524
Cinebench CPU 342.39
Cinebench CPU 340.95
Cinebench CPU 341.38
Cinebench GPU 58.11
Cinebench GPU 57.47
Cinebench GPU 57.79
数字之前的文字不需要说那些确切的事情,而是类似的事情。
答案 0 :(得分:0)
Stack Overflow不是免费的代码编写服务。但是我为这项任务写了一个小的评论批处理文件。
使用存储在文件ListFile.txt
中的示例行,下面的批处理代码生成文件ExtractedData.txt
,其中包含以下行:
Cinebench CPU 340.95
Cinebench CPU 341.38
Cinebench CPU 342.39
Cinebench GPU 57.47
Cinebench GPU 57.79
Cinebench GPU 58.11
Geekbench 3646 7510
Geekbench 3654 7511
Geekbench 3657 7524
以下是批次代码:
@echo off
rem Exit batch file if list file with data to extract does not exist.
if not exist "ListFile.txt" goto :EOF
rem Enabled delayed expansion needed in FOR loop for string detection.
setlocal EnableExtensions EnableDelayedExpansion
rem Define and delete temporary file used for the extracted data before sort.
set "TempFile=%TEMP%\%~n0.tmp"
del "%TempFile%" 2>nul
rem Process each non empty line from the list file and evaluate the data
rem after second colon which means ignoring the file name with path.
rem The IF conditions are string comparisons which compare data with
rem string to check removed case-insensitive with complete data.
rem For example the second condition with string Rendering removed
rem from data string is not equal the unmodified data string if the
rem data string contains the string Rendering in any case.
for /F "usebackq tokens=2* delims=:" %%K in ("ListFile.txt") do (
set "Data=%%L"
if "!Data:Geekbench Score=!" NEQ "%%L" (
for /F "tokens=3,4" %%A in ("%%L") do echo Geekbench %%A %%B>>"%TempFile%"
) else if "!Data:Rendering=!" NEQ "%%L" (
for /F "tokens=5" %%A in ("%%L") do echo Cinebench CPU %%A>>"%TempFile%"
) else if "!Data:Shading=!" NEQ "%%L" (
for /F "tokens=4" %%A in ("%%L") do echo Cinebench GPU %%A>>"%TempFile%"
)
)
rem Were any data extracted from list file? Yes, sort the lines
rem alphabetic (not numeric) and delete the temporary file.
if exist "%TempFile%" (
%SystemRoot%\System32\sort.exe "%TempFile%" /O "ExtractedData.txt"
del "%TempFile%"
)
endlocal
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
call /?
有关%~n0
的说明(没有路径和扩展名的批处理文件的名称)。del /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
sort /?
另请参阅Microsoft文章Using command redirection operators。
答案 1 :(得分:0)
这是一种更简单的方法:
@echo off
setlocal EnableDelayedExpansion
rem Initialize "score", "pts" and "fps" result strings
set "score="
set "pts="
set "fps="
rem Process all lines in the input file and inspect just tokens 4, 5 and 6
for /F "tokens=4,5,6" %%a in (input.txt) do (
rem Accumulate each result into the appropriate variable
if "%%c" equ "pts" (
set "pts=!pts! %%b"
) else if "%%b" equ "fps" (
set "fps=!fps! %%a"
) else (
set score=!score! "%%a %%b"
)
)
rem Show the results
for %%a in (%score%) do echo Geekbench %%~a
for %%a in (%pts%) do echo Cinebench CPU %%a
for %%a in (%fps%) do echo Cinebench GPU %%a
输出:
Geekbench 3646 7510
Geekbench 3654 7511
Geekbench 3657 7524
Cinebench CPU 342.39
Cinebench CPU 340.95
Cinebench CPU 341.38
Cinebench GPU 58.11
Cinebench GPU 57.47
Cinebench GPU 57.79