-1

I am trying to strip out unwanted text in a .txt file and its not the entire line. Here is an example of text:

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

I need to only keep the FPS, pts, and Score sections (The numbers) but this is what is inside the text file. If I have to split it using findstr then stripping stuff that's fine with me.

Please let me know if you can think of anything. I know how to do it in Python but we are not allowed to install any extra tools for this.

It needs to be something like this for the output.

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

The text before the numbers don't need to say those exact things but something like that.

Danny Brito
  • 21
  • 1
  • 7
  • 1
    Could you please show the EXACT output you want from the input example you provided. Please edit your question with that information. – Squashman Feb 01 '16 at 19:50
  • Possible duplicate of [Extract specific text from text file using batch](http://stackoverflow.com/questions/21260587/extract-specific-text-from-text-file-using-batch). Please make an effort to search first before asking a new question, as chances are quite good that it's been asked (and answered) before here. Thanks. – Ken White Feb 01 '16 at 19:53
  • Ken, I did search for it but I did not find that example and that example doesn't seem to be what I need. as I don't really have the same text all around. What I mean is there is no delimiter that I have that is possible for me to go by since its all different. – Danny Brito Feb 01 '16 at 20:06
  • Please provide sufficient source data to derive your expected output. – Magoo Feb 01 '16 at 20:13

2 Answers2

0

Stack Overflow is not a free code writing service. But I wrote nevertheless a small commented batch file for this task.

With the example lines stored in file ListFile.txt the batch code below produces the file ExtractedData.txt with following lines:

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

Here is the batch code:

@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

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? for an explanation of %~n0 (name of batch file without path and extension).
  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • sort /?

See also the Microsoft article about Using command redirection operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

This is a simpler approach:

@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

Output:

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
Aacini
  • 65,180
  • 12
  • 72
  • 108