Computing.Net > Forums > Programming > Capturing/Filtering Blocks of Text:

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Capturing/Filtering Blocks of Text:

Reply to Message Icon

Name: mattdw
Date: March 6, 2008 at 09:43:07 Pacific
OS: Windows XP Professional
CPU/Ram: 2.80 GHz
Product: HP SFF
Comment:

Is there a way through batch scripting to capture/filter specific blocks of text (four lines each) from a text file when the text I am looking for is interspersed throughout the document?

SysInternals PSFile utility is able to create a list of files opened remotely on a computer, shown below. My goal is I'd like to filter this down to just entries made by a particular user (say MATTDW) and pipe this to a new text file.

There are four lines that pertain to each MATTDW entry as seen here, but the username is the only constant so I'm not sure if Window's FIND.exe, is the tool to do this. Thanks much!

[1514208] \PIPE\spoolss
User: MATTDW
Locks: 0
Access: Read

---

Windows XP Professional
3.39 GHz

PsFile v1.01 - local and remote network file lister
Copyright (C) 2001 Mark Russinovich
Sysinternals - www.sysinternals.com

Files opened remotely on presrvr:

[1495506] E:\Students\2012\12345\filename.fla
User: 12345
Locks: 3
Access: Read Write
[1506409] \PIPE\spoolss
User: BETTYB
Locks: 0
Access: Read Write
[1506410] \PIPE\winreg
User: BETTYB
Locks: 0
Access: Read Write
[1509090] E:\Main\Staff\JANER\Private\FOLDERNAME\filename.doc
User: JANER
Locks: 0
Access: Read Write
[1511096] E:\Main\Staff\JILLP\Private
User: JILLP
Locks: 0
Access: Read
[1511365] E:\Main\Staff\JOEB\Public\Science 8
User: JOEB
Locks: 0
Access: Read
[1512423] E:\Main\Staff\JOEB\Public\Science 8\Astronomy\Rocketry\filename.xls
User: JOEB
Locks: 0
Access: Read Write
[1512679] E:\Main
User: MATTDW
Locks: 0
Access: Read
[1512853] E:\Main\Staff\BETHC\Private\Documents 05 06\Odyssey
User: BETHC
Locks: 0
Access: Read
[1513379] E:\Students\2012\23456
User: 23456
Locks: 0
Access: Read
[1513380] E:\Students\2012\34567
User: 34567
Locks: 0
Access: Read
[1513643] E:\Students\2012\45678\filename.pub
User: 45678
Locks: 0
Access: Read Write
[1513715] E:\Students\2013\56789\filename.ppt
User: 56789
Locks: 0
Access: Read
[1514082] E:\Main
User: BILLYG
Locks: 0
Access: Read
[1514208] \PIPE\spoolss
User: MATTDW
Locks: 0
Access: Read

-Matt- :-)



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: March 6, 2008 at 10:06:17 Pacific
Reply:

If you're open to options other than a batch file, here's a Perl solution.

#!perl

use strict;
use warnings;
use Tie::File;

tie my @array, 'Tie::File', 'sysfile.txt' or die $!;

for my $i ( 0..$#array ) {
if ( $array[$i] =~ /User: MATTDW/ ) {
print join("\n", @array[$i-1..$i+2]) . "\n";
}
}



0

Response Number 2
Name: Mechanix2Go
Date: March 6, 2008 at 13:36:57 Pacific
Reply:

:: get 4 line blocks to new files

@echo off
setLocal EnableDelayedExpansion

if exist outfile.* del outfile.*

for /f "tokens=* delims= " %%a in (matt.txt) do (
echo %%a | find "[" > nul
if not errorlevel 1 set /a N+=1
echo %%a >> outfile.!N!
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: mattdw
Date: March 6, 2008 at 20:52:36 Pacific
Reply:

Thanks Mechanix2Go, I'll play around with the findstr command with the /S and /M switches more tomorrow. From there I should be able to delete the unneeded outfile.# files (those not containing a match) and then can use the FOR command to load all MATTDW entries back into one text file. I think this will work.

FishMonger, thanks. I haven't had the occasion to play with PERL much but I'm always interested in seeing how other programming languages handle similar types of tasks. In this case I think it'd be best to stick with batch scripting since this will eventually be integrated back in to another batch script I wrote yesterday -- plus, I simply don't know enough about PERL at this point in time (although I'd love to dive into this more in the future) to modify the above to work with the other pieces I am working with. I'm saving the code to look at more closely though -- so thanks for your response!

Much appreciated,

-Matt- :-)


0

Response Number 4
Name: Mechanix2Go
Date: March 6, 2008 at 21:04:34 Pacific
Reply:

:: get 4 line blocks to new files
:: and get MATTDW blocks into another new file

@echo off > MATTDW.txt
setLocal EnableDelayedExpansion

if exist outfile.* del outfile.*

for /f "tokens=* delims= " %%a in (matt.txt) do (
echo %%a | find "[" > nul
if not errorlevel 1 set /a N+=1
echo %%a >> outfile.!N!
)
for /f "tokens=* delims= " %%m in ('dir/b outfile.*') do (
find "MATTDW" < %%m > nul
if not errorlevel 1 type %%m >> MATTDW.txt
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: mattdw
Date: March 7, 2008 at 09:33:34 Pacific
Reply:

Mechanix2Go,

Thank you very much!

-Matt- :-)


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: March 8, 2008 at 00:02:57 Pacific
Reply:

here's one using gawk for windows

[code]
BEGIN{RS="["}$0 ~ /MATT/{ print "["$0 }
[/code]
save it as filter.awk and from command line

c:> gawk -f filter.awk myfile


0

Sponsored Link
Ads by Google
Reply to Message Icon

VB6 - Passing 2 vars to s... Help with JAR files



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Capturing/Filtering Blocks of Text:

Deleting a block of text with sed www.computing.net/answers/programming/deleting-a-block-of-text-with-sed/16729.html

Delete first line of text www.computing.net/answers/programming/delete-first-line-of-text/13312.html

Batch to del first x lines of text www.computing.net/answers/programming/batch-to-del-first-x-lines-of-text/16887.html