Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 GHzPsFile v1.01 - local and remote network file lister
Copyright (C) 2001 Mark Russinovich
Sysinternals - www.sysinternals.comFiles 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- :-)

If you're open to options other than a batch file, here's a Perl solution.
#!perluse 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";
}
}

:: get 4 line blocks to new files
@echo off
setLocal EnableDelayedExpansionif 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

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- :-)

:: get 4 line blocks to new files
:: and get MATTDW blocks into another new file@echo off > MATTDW.txt
setLocal EnableDelayedExpansionif 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

here's one using gawk for windows
[code]
BEGIN{RS="["}$0 ~ /MATT/{ print "["$0 }
[/code]
save it as filter.awk and from command linec:> gawk -f filter.awk myfile

![]() |
VB6 - Passing 2 vars to s...
|
Help with JAR files
|

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