Find a String

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
testingbatch
Posts: 7
Joined: 19 Feb 2019 09:13

Find a String

#1 Post by testingbatch » 08 Mar 2019 03:41

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.

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

Re: Find a String

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

testingbatch
Posts: 7
Joined: 19 Feb 2019 09:13

Re: Find a String

#3 Post by testingbatch » 08 Mar 2019 08:42

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?
Last edited by testingbatch on 08 Mar 2019 10:18, edited 1 time in total.

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

Re: Find a String

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

testingbatch
Posts: 7
Joined: 19 Feb 2019 09:13

Re: Find a String

#5 Post by testingbatch » 08 Mar 2019 09:54

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Find a String

#6 Post by Squashman » 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.

testingbatch
Posts: 7
Joined: 19 Feb 2019 09:13

Re: Find a String

#7 Post by testingbatch » 08 Mar 2019 10:23

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Find a String

#8 Post by Squashman » 08 Mar 2019 10:28

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?

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

Re: Find a String

#9 Post by aGerman » 08 Mar 2019 11:02

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

testingbatch
Posts: 7
Joined: 19 Feb 2019 09:13

Re: Find a String

#10 Post by testingbatch » 15 Mar 2019 05:36

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"

Post Reply