Reputation: 71
I run a third party command that displays error levels like this after it executes:
return code: 33
return code: 1
return code: 4
return code: 5
I pipe the result of the command into a log file. So test.log will have any combination of these lines for examples:
return code: 33
return code: 1
In my script I want to check the content of the log file and if the entry code was anything but 1 ( one means success ) I will do an action
I think the find command is what I have to use but not sure what syntax to use? Basically the logic should be, look into log file and if the error code is anything but 1 do some action.
Can anyone suggest how to write a code to check this?
Thanking you in advance Matt
Upvotes: 1
Views: 128
Reputation: 130919
The following will determine if there exists at least one return code in the log file that is not 1:
findstr /rxc:"return code: [0-9]*" test.log | findstr /ev ": 1" >nul && echo Do something
The following loop allows you to take action on each non 1 return code:
for /f "tokens=3 delims=: " %%A in (
'findstr /rxc:"return code: [0-9]*" test.log ^| findstr /ev ": 1"'
) do (
echo return code = %%A
echo So do something about it
)
If it is not matching because of extra spaces, try:
findstr /rxc:" *return code: *[0-9]* *" test.log | findstr /ev ": 1" >nul && echo Do something
Upvotes: 1
Reputation: 37129
Given a file test.log containing:
return code: 33
return code: 1
return code: 4
return code: 5
Your batch script to analyze test.log would look something like this:
@echo off
for /F "delims=" %%i in ('findstr /e /v ": 1" test.log') do (
echo %%i
rem do some other command instead of echo
)
Findstr's /e switch match pattern at the end of the line and /v switch chooses everything except the match. The result will be:
return code: 33
return code: 4
return code: 5
EDIT below
If content of text is something like below and we also want to do something with text alternate: 1
,
return code: 33
return code: 1
return code: 4
return code: 5
alternate: 1
We would make a subtle change like so:
@echo off
for /F "delims=" %%i in ('findstr /e /v "return code: 1" test.log') do (
echo %%i
rem do some other command instead of echo
)
This way we extract everything but return code: 1
.
EDIT 2
To exit out of the loop after the first instance, do this:
@echo off
for /F "delims=" %%i in ('findstr /e /v "return code: 1" test.txt') do (
echo %%i
goto :eof
)
:eof
Upvotes: 0