Hi Experts
I don't know if this would be possible in a batch file using any of the internal Windows XP CMD commands, or calling any of the SP2/SP3 support tools. I have a vague recollection of a command or program that locates a text string or a line, or something like that, and ALSO includes a specified number of lines either side of it in the results. Problem is that I cannot recall whether this was a third party utility (eg. SysInteranls tool) or native Windows program.
What I need to do is find the lines containing asterisks in text files, as in the extract here:
Start Offset: 0x00000000
*** Marker: SOI (xFFD8) ***
OFFSET: 0x00000000
*** Marker: APP1 (xFFE1) ***
OFFSET: 0x00000002
length = 9214
Identifier = [Exif]
*** Marker: DQT (xFFDB) ***
Define a Quantization Table.
OFFSET: 0x00002402
Table length = 132
.... BUT, as well as finding the lines with the ***, I also want to capture the line above and two lines below all instances found. Taking the extract above, I need to capture all "*** Marker:" lines, any lines immediately above referencing an Offset value, and the lines immediately below the Marker line referencing an Offset value (always the 1st or 2nd line below). So, I figured that One line above, and Two lines below those lines with asterisks would capture all of what I need, and I can clean up superfluous content afterwards.
Finding the lines containing asterisks and redirecting the found lines in each text file to a new file with the same name, plus a suffix, is the easy part:
for %%a in ("*.txt") do type %%a | find /i "*" >> "%%~ni_Markers.txt"
I've looked at FINDSTR as well as FIND, and it doesn't do what I need either.
I tried to write some nested FOR statements to also search for "offset:", but got totally lost in the spaghetti code.
Some of the *.txt files run to over 400 pages at size 10 font when pasted into MS Word, so this should give you an idea why it's not really feasible to manually scroll through a number of text files and delete content, even though 95% of the content comprises huge blocks of unwanted content.
At a wild guess, a VBS file might be able to do this on its own, or even by calling and using an MS Word document with a Macro to do the dirty work, but I had hoped there might be a more fluent way using a standalone utility program.
Does anybody know of a method to INCLUDE a pre-defined number of adjacent lines with the line containing the text string searched for?
Thanks
Bill
by: SuperdavePosted on 2008-12-13 at 19:25:49ID: 23166410
GNU grep will do that. See the -A and -B options. You should easily be able to find a Windows binary to download.