for loop - Batch script to grab lines with findstr without filepath -


i've got log file monitors large system including requests , acknowledgements. objective able to:

1. loop through script , lines requests & acknowledgements happen

2. pull entire lines of importance strings , store them variables string modifying output somewhere else.

here's have far:

@echo off setlocal setlocal enabledelayedexpansion setlocal enableextensions  :: lets today's date, formatted way abcd file named /f "tokens=1-5 delims=/ " %%d in ("%date%") set targetdate=%%f-%%d-%%e :: set targetfile name set abcdlogsfile=c:\users\me\documents\monitoring_file_for_jim\abcdfix*%targetdate%.log ::****scrapped original approach***** set "ackfoundcount=0" set "reqfoundcount=0" ::get lines acks /f delims^=^ eol^= %%a in ('findstr /c:"\<ack\>" "%abcdlogsfile%"') (     set /a "ackfoundcount+=1"     setlocal enabledelayedexpansion     %%n in (!ackfoundcount!) (         endlocal         set "ackfound%%n=%%a"     ) ) ::get lines requests /f delims^=^ eol^= %%b in ('findstr /c:"reqsingle" "%abcdlogsfile%"') (     set /a "reqfoundcount+=1"     setlocal enabledelayedexpansion     %%n in (!reqfoundcount!) (         endlocal         set "reqfound%%n=%%b"     ) )  setlocal enabledelayedexpansion /l %%n in (1,1,2 %reqfoundcount%) echo req %%n found= !reqfound%%n! pause /l %%n in (1,1,2 %ackfoundcount%) echo ack %%n found= !ackfound%%n! endlocal 

edit 2 dbenham

the roundabout way trying accomplish before totally unnecessary. questions , answer here:

'findstr' multiple search results (batch)

i've got script working similarly. however, i'm curious if possible findstr output without filepath @ beginning. need substring out timestamp in log, first 12 characters of each line (without filepath). output prefixed path, , while path log in production, safer try , way. @ time script run, there 1 or 2 reqs , acks each, why store found. it's not necessary think reassuring see 2 if there two. here output looks acks , reqs alike:

c:\users\me\documents\monitoring_file_for_jim\abcdfix 2015-04-01.log:2015-03-26 07:00:11,028 info  etc... 

i'm thinking if strip filepath off start, i'd need just timestamps of events

for /l %%n in (1,1,1 %reqfoundcount%) echo req %%n occurred at: !reqfound%%n:~0,12! >> morningackchecks.txt /l %%n in (1,1,1 %ackfoundcount%) echo ack %%n occurred at: !ackfound%%n:~0,12! >> morningackchecks.txt 

i suspect not skip work because you iterating delimited list of line numbers statement, means number in variable. problem is, cannot include variables or (delayed expansion) when specifying skip value, or other option. batch parser evaluates options before variables expanded, couldn't possibly work. normal expansion can used when including variable part of options.

but don't understand why think need line numbers @ all. findstr able parse out lines want. use /f iterate each matching line. each line, define variable containing line content, , use substring operations parse out desired values.

but can offer alternative think make life easier. jrepl.bat sophisticated regular expression text processor identify lines , parse out , transform desired values, in 1 pass. jrepl.bat hybrid jscript/batch script runs natively on windows machine xp onward.

if knew input looked like, , desired output is, knock simple solution using jrepl.bat. or read extensive built in documentation , figure out yourself.

documentation accessed command line via jrepl /?. might want pipe output through more 1 screen of @ time. never because command line console configured large output buffer, can scroll see past output.


edit - in response comment , updated question

here relevant snippets of code causing problem.

set abcdlogsfile=c:\users\me\documents\monitoring_file_for_jim\abcdfix*%targetdate%.log findstr /c:"\<ack\>" "%abcdlogsfile%" findstr /c:"reqsingle" "%abcdlogsfile% 

the issue abcdlogsfile definition includes wildcard, causes findstr prefix each matching line full path file name match occurred.

i have simple solution - change definition of abcdlogsfile follows:

set "abcdlogsfile=c:\users\me\documents\monitoring_file_for_jim\abcdfix<%targetdate%.log" 

explanation

my solution relies on 2 undocumented features

1) undocumented file mask wildcards.

  • < - similar *
  • > - similar ?

these symbols used redirection, must either quoted or escaped if want use them file mask wildcards.

we discuss undocumented feature @ dostips - dir undocumented wildcards. sprinkled throughout thread (and link) example use cases.

i document understanding of how non-standard wildcards work @ http://www.dostips.com/forum/viewtopic.php?p=39420#p39420

2) findstr works non-standard wildcards

findstr prefix each matching line file name (and possibly path) if of following conditions occur

  • the /m option used
  • the /f option used
  • multiple input files explicitly listed on command line
  • multiple input files implied via file mask @ least 1 * or ? wildcard on command line

your getting file path prefix because of last trigger - * in file mask.

but can use < instead same result, except non-standard wildcards not trigger file prefix in output.

problem solved :-)

i talk findstr feature @ http://www.dostips.com/forum/viewtopic.php?p=39464#p39464.

some day hope update what undocumented features , limitations of windows findstr command? post tasty little tidbit.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -