Page 1 of 1

Find a String

Posted: 08 Mar 2019 03:41
by testingbatch
Hello everyone, thanks for the help on my first topic here, it was really helpful.

So i have this string on my logs.txt and when we have a build on our websites it will put a new string on the txt file.

String example:
The following request has finished: Request type: ScanRequestRequest ID: 11Assessee name: Teste.Zip
Well this is my string and my problem is on the label ScanRequest ID because is always a random number (the rest of the string is always the same). What is the best way to catch this string?

I'm working with FINDSTR and i've been using the findstr /C: command but when the program gets the ":" i think the command can't process that very well.

Thanks in advance.

Re: Find a String

Posted: 08 Mar 2019 08:20
by aGerman
So as I understood you need to match the whole line where the number may differ.
Untested:

Code: Select all

type "logs.txt"|findstr /rc:"The following request has finished: Request type: ScanRequestRequest ID: [0-9][0-9]*Assessee name: Teste\.Zip"
If you are sure that there are no spaces in front or behind the line then you could restrict it even more using options /RXC:

Steffen

Re: Find a String

Posted: 08 Mar 2019 08:42
by testingbatch
aGerman wrote:
08 Mar 2019 08:20
So as I understood you need to match the whole line where the number may differ.
Untested:

Code: Select all

type "logs.txt"|findstr /rc:"The following request has finished: Request type: ScanRequestRequest ID: [0-9][0-9]*Assessee name: Teste\.Zip"
If you are sure that there are no spaces in front or behind the line then you could restrict it even more using options /RXC:

Steffen
Sorry Steffen, i made a mistake on the output. It isn't a whole line, sorry. That's why i asked if using a For would be a better option.
The following request has finished:
Request type: ScanRequest
Request ID: 10
Assessee name: CEB.PT
I'm using something like this: findstr /C:"The following request has finished: " teste.log and i use your alternative aswell but only works until the first break page. So, a for cicle where would be the best approach, right?

Re: Find a String

Posted: 08 Mar 2019 09:11
by aGerman
I still don't get what your goal is. Do you want to filter out these four lines? Or do you want to extract the ID number out of your log file?

Re: Find a String

Posted: 08 Mar 2019 09:54
by testingbatch
aGerman wrote:
08 Mar 2019 09:11
I still don't get what your goal is. Do you want to filter out these four lines? Or do you want to extract the ID number out of your log file?
Just to extract the four lines.

Re: Find a String

Posted: 08 Mar 2019 10:01
by Squashman
You need to show us a full example of the log file.
testingbatch wrote:
08 Mar 2019 08:42
That's why i asked if using a For would be a better option.
You never asked this in your original question.

Re: Find a String

Posted: 08 Mar 2019 10:23
by testingbatch
Squashman wrote:
08 Mar 2019 10:01
You need to show us a full example of the log file.
testingbatch wrote:
08 Mar 2019 08:42
That's why i asked if using a For would be a better option.
You never asked this in your original question.
My text.log has about 9k lines, and example:

Code: Select all

01-02-2019 16:54
The following request has started: 
Request type: ScanRequest
Request ID: 10
Assessee name: Test.Zip

01-02-2019 21:11
The following request has finished: 
Request type: ScanRequest
Request ID: 10
Assessee name: Test.Zip

04-02-2019 17:12
The following request has started: 
Request type: ScanRequest
Request ID: 10
Assessee name: Test.Zip

04-02-2019 22:09
The following request has finished: 
Request type: ScanRequest
Request ID: 10
Assessee name: Test.Zip
The main goal is to find the 4 strings only about when the request is finished (ignore the 4 lines about the start).

Thanks in advance for the help guys, really appreciate it.

About the for, yeah, you right, my bad.

Re: Find a String

Posted: 08 Mar 2019 10:28
by Squashman
testingbatch wrote:
08 Mar 2019 10:23
The main goal is to find the 4 strings only about when the request is finished (ignore the 4 lines about the start).
You don't want the date and time stamp?

Re: Find a String

Posted: 08 Mar 2019 11:02
by aGerman
Maybe something like that …

Code: Select all

@echo off &setlocal
set "infile=teste.log"

setlocal EnableDelayedExpansion
<"!infile!" (
  for /f %%i in ('type "!infile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    for /f "tokens=5" %%k in ("!line!") do if "%%k"=="finished:" set "found=1"
    if not defined line (set "found=") else if defined found echo(!line!
  )
)

pause
Steffen

Re: Find a String

Posted: 15 Mar 2019 05:36
by testingbatch
I found a nice way to get complete this task. I just transform the document in just one line and check if there was some update today, if yes, the program grep all the lines and copy to a new txt file and with the for loop is going to transform the multiple string in just one.

Code: Select all

for /f "usebackqdelims=" %%i in ("ouncelogs.txt") do @<nul set /p"=%%i">>"output.txt"