How to get the number after fail in a string inside a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

How to get the number after fail in a string inside a file

#1 Post by goodywp » 24 Mar 2022 09:53

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 ?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get the number after fail in a string inside a file

#2 Post by aGerman » 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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to get the number after fail in a string inside a file

#3 Post by Compo » 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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get the number after fail in a string inside a file

#4 Post by aGerman » 24 Mar 2022 14:17

As to my understanding, it's only one long line, Compo ¯\_(ツ)_/¯

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to get the number after fail in a string inside a file

#5 Post by goodywp » 25 Mar 2022 07:40

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.

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to get the number after fail in a string inside a file

#6 Post by goodywp » 25 Mar 2022 07:41

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!

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to get the number after fail in a string inside a file

#7 Post by Compo » 26 Mar 2022 14:48

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get the number after fail in a string inside a file

#8 Post by aGerman » 27 Mar 2022 03:20

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to get the number after fail in a string inside a file

#9 Post by Aacini » 28 Mar 2022 09:59

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

Post Reply