jrepl : regular expression

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mister_ik
Posts: 2
Joined: 15 Dec 2016 06:48

jrepl : regular expression

#1 Post by Mister_ik » 15 Dec 2016 08:01

I am new to reg-ex and I have a lot of difficulties to solve the following problem:

I have a errorlog like this:

Code: Select all

14-12-2016 12:15:23 ABCABC WPU8D0006316 PROGRAM:ERROR MESSAGE
14-12-2016 13:35:23 DEFGHI WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE
14-12-2016 14:25:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE
14-12-2016 15:35:23 DEFGHI WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE
14-12-16 16:35:23 DEFGHI WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE  ERROR MESSAGE ERROR MESSAGE
14-12-2016 17:45:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE
15-12-2016 12:15:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE
15-12-16 13:35:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE
15-12-16 14:25:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE
15-12-2016 15:35:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE
15-12-2016 16:35:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE ERROR MESSAGE  ERROR MESSAGE ERROR MESSAGE
15-12-16 17:45:23 ABCABC WPU8D0006316 PROGRAM ERROR MESSAGE

This logfile is growing each day. I would like to have a batch script which create a new file with only the lines from a starting point till the end of the file. For example from '14-12-2016 17:45:23' till the end of file. So I can do some analyses on only the newest errors. The next time I will run this script it should create a new file from 15-12-2016 17:45:23 till the end of the new error file.

I found the tool Jrepl.bat which looks like it can do the job. But this tool has a lot of options i don't understand and i cant figure out what the regular expression need to be. Hopefully anyone else can? Thanks in advance.

Code: Select all

SET InFile=c:\logfile.txt
SET OutFile=c:\logfile_NEW.txt
SET ZoekText=14-12-2016 17:45:23 (i will put this in a external file)

call %SrcDIR%\jrepl "%ZoekText%" "$1"  /f "%InFile%" /o "%OutFile%"

Mister_ik
Posts: 2
Joined: 15 Dec 2016 06:48

Re: jrepl : regular expression

#2 Post by Mister_ik » 21 Dec 2016 08:47

anyone? It would really help me! Thanks in advance.

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

Re: jrepl : regular expression

#3 Post by aGerman » 21 Dec 2016 13:50

I'm not sure if you would even need regex. What about that code

Code: Select all

@echo off &setlocal
set "InFile=logfile.txt"
set "OutFile=logfile_NEW.txt"
set /p "ZoekText="<"zoek.txt"

setlocal EnableDelayedExpansion
set "len=0"
set "check=A!ZoekText!"
for /l %%i in (12 -1 0) do (
  set /a "len|=1<<%%i"
  for %%j in (!len!) do if "!check:~%%j,1!"=="" set /a "len&=~1<<%%i"
)
set "check="
<"!InFile!" >"!OutFile!" (
  for /f %%i in ('type "!InFile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" & set /p "line="
    if not defined check (
      if "!line:~,%len%!"=="!ZoekText!" (set "check=1" &echo(!line!)
    ) else echo(!line!
  )
)

... where zoek.txt contains only one line with the date and time.

Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: jrepl : regular expression

#4 Post by dbenham » 21 Dec 2016 14:06

This should be enough to get you going:

Code: Select all

call jrepl "^" "" /k 0 /inc "/^14-12-2016/:-1" /f c:\logfile.txt /o c:\logfile_new.txt


I agree with aGerman that you shouldn't need regex, let alone JREPL

If you only want to see lines that begin with the given date:

Code: Select all

findstr /b "14-12-2016" c:\logfile.txt >c:\logfile_new.txt

If you must include lines that don't begin with the date, but occur after the first instance of the date:

Code: Select all

@echo off
setlocal
set "start="
for /f "delims=:" %%N in ('findstr /b "14-12-2016" c:\logfile.txt ') do (
  set /a start=%%N-1
  goto :break
 )
if defined start more +%start% c:\logfile.txt >c:\logfile_new.txt


Dave Benham

Post Reply