Page 1 of 1
How to get the number after fail in a string inside a file
Posted: 24 Mar 2022 09:53
by goodywp
Hi
I got a file (test.txt) which contain a string as below: (Actually it is one line below is word wrap)
window.output["stats"] = [[{"elapsed":"01:03:51","fail":8,"label":"Critical Tests","pass":56},{"elapsed":"01:03:51","fail":8,"label":"All Tests","pass":56}],[],[{"elapsed":"01:04:00","fail":8,"id":"s1","label":"AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413","name":"AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413","pass":56},{"elapsed":"00:02:21","fail":0,"id":"s1-s1","label":"AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.CHC Initialisation","name":"CHC Initialisation","pass":3},{"elapsed":"00:27:19","fail":0,"id":"s1-s2","label":"AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.Chase Manual Entry","name":"Chase Manual Entry","pass":28},{"elapsed":"00:33:04","fail":8,"id":"s1-s3","label":"AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.Chase Swipe Card","name":"Chase Swipe Card","pass":24},{"elapsed":"00:01:16","fail":0,"id":"s1-s4","label":"AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.CHC auto settlement","name":"CHC auto settlement","pass":1}]];
The position of "fail" should be same but the failed number can be two digits (above example is 8 ).
How can I get this failed number from the above string ?
If I use Extract a Substring by Position I come across the failed number could be two digits..
Other than that I can not think any better way to extract this failed number ?
Re: How to get the number after fail in a string inside a file
Posted: 24 Mar 2022 13:27
by aGerman
If you're sure that the number is always at the same position, read two characers, and if the second is a comma, use only the first.
You know what, this is already ugly (you should rather use a JSON processor). So, let's make it even worse ...
Code: Select all
<"test.txt" set /p "line="
2>nul set /a "num=%line:~56%"
SET /A stops at the first character which can't be converted to an integer. Using 2>nul we ignore the error message that we get in this case.
Steffen
Re: How to get the number after fail in a string inside a file
Posted: 24 Mar 2022 14:10
by Compo
Or selecting only a line matching that which you've posted, and regardless of the postition or the number of digits…
Code: Select all
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "failNumber=" & For /F "Delims=" %%G In ('%SystemRoot%\System32\findstr.exe
/IRC:"window\.output\[\"stats\"\] = \[\[{.*\"fail\":[0123456789][0123456789]*,"
"test.txt" 2^>NUL') Do (Set "matchedLine=%%G" & SetLocal EnableDelayedExpansion
For /F "Delims=:," %%H In ("!matchedLine:*"fail"=!"
) Do EndLocal & Set "failNumber=%%H")
Set failNumber 2>NUL
Pause & EndLocal & GoTo :EOF
Re: How to get the number after fail in a string inside a file
Posted: 24 Mar 2022 14:17
by aGerman
As to my understanding, it's only one long line, Compo ¯\_(ツ)_/¯
Re: How to get the number after fail in a string inside a file
Posted: 25 Mar 2022 07:40
by goodywp
aGerman wrote: ↑24 Mar 2022 13:27
If you're sure that the number is always at the same position, read two characers, and if the second is a comma, use only the first.
You know what, this is already ugly (you should rather use a JSON processor). So, let's make it even worse ...
Code: Select all
<"test.txt" set /p "line="
2>nul set /a "num=%line:~56%"
SET /A stops at the first character which can't be converted to an integer. Using 2>nul we ignore the error message that we get in this case.
Steffen
Clearly and simplicity instead of ugly
and most important it works. I tested 8 cases and works very well.
Re: How to get the number after fail in a string inside a file
Posted: 25 Mar 2022 07:41
by goodywp
Compo wrote: ↑24 Mar 2022 14:10
Or selecting only a line matching that which you've posted, and regardless of the postition or the number of digits…
Code: Select all
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "failNumber=" & For /F "Delims=" %%G In ('%SystemRoot%\System32\findstr.exe
/IRC:"window\.output\[\"stats\"\] = \[\[{.*\"fail\":[0123456789][0123456789]*,"
"test.txt" 2^>NUL') Do (Set "matchedLine=%%G" & SetLocal EnableDelayedExpansion
For /F "Delims=:," %%H In ("!matchedLine:*"fail"=!"
) Do EndLocal & Set "failNumber=%%H")
Set failNumber 2>NUL
Pause & EndLocal & GoTo :EOF
I should try this one as well to see and will update the results soon. Thanks!
Yes it also works!
Re: How to get the number after fail in a string inside a file
Posted: 26 Mar 2022 14:48
by Compo
You could optionally modify the command to get the first pass number too:
Code: Select all
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "passNumber=" & For /F "Delims=" %%G In ('%SystemRoot%\System32\findstr.exe /IR
/C:"window\.output\[\"stats\"\] = \[\[{.*\"[fp]a[is][ls]\":[0123456789][0123456789]*[,}]"
"test.txt" 2^>NUL') Do (Set "matchedLine=%%G" & SetLocal EnableDelayedExpansion
Set "matchedLine=!matchedLine:*"pass"=!" & For /F "Delims=:,}" %%H In (
"!matchedLine:*"pass"=!") Do EndLocal & Set "passNumber=%%H")
Set passNumber 2>NUL
Pause & EndLocal & GoTo :EOF
Re: How to get the number after fail in a string inside a file
Posted: 27 Mar 2022 03:20
by aGerman
A JScript hybrid would be pretty straightforward here. The number can be accessed directly as submatch of a regex pattern.
Code: Select all
@if (0)==(0) echo off &setlocal
for /f %%i in ('cscript //nologo //e:jscript "%~fs0" "test.txt"') do set "n=%%i"
echo %n%
pause
goto :eof @end
WScript.Echo(/"fail":(\d+)/.exec(new ActiveXObject('Scripting.FileSystemObject').OpenTextFile(WScript.Arguments(0)).ReadAll())[1]);
Steffen
Re: How to get the number after fail in a string inside a file
Posted: 28 Mar 2022 09:59
by Aacini
If you add a few simple manipulations, you can access all fields from all tests:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1* delims== " %%a in (test.txt) do set "line=%%b" & set "line=!line:~3,-4!" & set "line=!line:],[],[=,!"
set "i=0"
for %%N in (^"^
%Do NOT remove%
^") do (
for /F "delims=" %%a in ("!line:},{=%%~N!") do (
set /A i+=1
echo/
echo Test # !i!
for %%b in (%%a) do for /F "tokens=1* delims=:" %%x in ("%%b") do echo %%~x=%%y
)
)
Output:
Code: Select all
Test # 1
elapsed="01:03:51"
fail=8
label="Critical Tests"
pass=56
Test # 2
elapsed="01:03:51"
fail=8
label="All Tests"
pass=56
Test # 3
elapsed="01:04:00"
fail=8
id="s1"
label="AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413"
name="AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413"
pass=56
Test # 4
elapsed="00:02:21"
fail=0
id="s1-s1"
label="AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.CHC Initialisation"
name="CHC Initialisation"
pass=3
Test # 5
elapsed="00:27:19"
fail=0
id="s1-s2"
label="AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.Chase Manual Entry"
name="Chase Manual Entry"
pass=28
Test # 6
elapsed="00:33:04"
fail=8
id="s1-s3"
label="AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.Chase Swipe Card"
name="Chase Swipe Card"
pass=24
Test # 7
elapsed="00:01:16"
fail=0
id="s1-s4"
label="AXIUM_DX8000_A591-09198-0101000_04381_CHC_00413.CHC auto settlement"
name="CHC auto settlement"
pass=1
Antonio