B. R. 'BeAr' Ederson said:
Specifically, I am looking for a utility to extract lines with target
text, together with 2 lines preceding each one, and write that to an
output file. Main use: For Antivir logs, target text as [DETECTION]
Most Grep programs support different values for leading and trailing
context lines. GNU grep does in any case.
Thanks, perfect! The GNU Grep does exactly what I want!
grep.exe -F -B2 -A0 "[DETECTION]" antivir.log >antivir.new
:: -F --fixed-strings PATTERN a set of newline-separated strings
:: Context control:
:: -B, --before-context=NUM print NUM lines of leading context
:: -A, --after-context=NUM print NUM lines of trailing context
:: -C, --context[=NUM] print NUM (default 2) lines of output context
:: unless overridden by -A or -B
Btw, on subject of my immediate use:
My Antivir logs had been unreadable to where I fall asleep before really
reading much. Making my scans then sort of pointless, since I have it take
zero action and merely write to file the items that attract its interest.
The GNU Grep magic gives me a consolidated report instead. Eliminating
all of Antivir's distracting chatter.
Next step, further refinement, that I might take will to be to look into
some sort of DIFF (file2-file1=new) strategy. To reduce the amount of
rereading I have to do on its huge number of false-positives (which result
esp with using its heuristic & related options).
You can get a full set of GNU utils for Windows here:
http://unxutils.sourceforge.net
This looks to be the best GNU Grep, in that it's current, and too, don't
have to be concerned with dependency on special dlls.
< begin meandering rambling (all caff'd up) >
I did have also stored on my disk an older port, from
<
http://www.interlog.com/~tcharron/grep.html>.
As well, had the one that comes from <
http://gnuwin32.sourceforge.net>.
Needs the special dlls. (The site's a great treasure trove though: lots
to download, lots of docs.)
Finally, another that again wants special dlls. It's from the "Cygwin on
a Floppy" package. Not sure home for that -- and quick web lookup makes me
wonder if the project has been basically abandoned? Took visit to the Cygwin
home page, and most of what I could spot was a single door -- into some long
labyrinth of installer hassle. No fast standalone downloads.
I'd done a search on my disk for programs with Grep or a variation of that
as their filenames. (I have a lot of unexamined cmd utils sitting around.)
Differently from the GNU Greps, the others that I had didn't do the neat
trick with the context lines.
The one exception in my local search was a program named f_grep. But it has
far less options, also didn't perform quite how I wanted for my purpose here
(output of file path and line numbers seems automatic and not optional).
I did puzzle over its identity. It says: "f_grep (Win32) version 2.70 (c)
Copyright IBM Corp. 1996-2002." Puzzlement since I don't tend to identify
IBM as being much involved in producing freeware. (?)
The way I got hold of this program was by downloading and running the DOS
file manager named F (fwin32.exe) from <
http://filemanager.free.fr>. The
f_grep.exe sort of spontaneously leaps from its pouch when that is launched.
(It might be that some developer involved there worked for IBM, perhaps that
would be an angle of theory to sort of explain the IBM (c) for f_grep?)
< /end meandering rambling >
Anyway. Thanks again, BeAR, for giving me the answer. I was not conscious
GNU Grep provided the context lines feature, fully customizable, to meet
my need exactly.