0: This was one of the first scripts I wrote. I needed to rapidly find strings in compressed IIS logfiles, and this was the result.
1:
2: It handles both compressed and uncompressed logfiles in the same directory, but does not handle ZIP files with more than one file inside.
3:
4: Any feedback welcomed.
5:
6: #
7: # Search.pl -- Search for arbitary string in logfiles
8: # Designed for a ActivePerl/Win32 environment
9: #
10: # Author: Joshua Thomas
11: # Last update: 10/24/2002
12: #
13: # 1.0: initial release
14: #
15:
16: use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
17: use Cwd;
18:
19: # Get our path and current working dir
20: # Expect the following args, ARGV[0] = [zip|log|all]
21: # ARGV[1] = [path], ARGV[2] = [phrase], ARGV[3] = [outfile]
22:
23: ($scope = $ARGV[0]) || &usage;
24: ($path = $ARGV[1]) || &usage;
25: ($phrase = $ARGV[2]) || &usage;
26: ($outfile = $ARGV[3]) || &usage;
27:
28: # Strip whitespace from args
29: $scope =~ s/\s+//;
30:
31: if ($scope != /(zip)|(log)|(all)/) { &usage; }
32:
33: # Move to working directory
34:
35: $cwd = getcwd();
36: chdir $path;
37:
38: # Open the master file that we write results out to
39:
40: open(OUTFILE, ">$outfile");
41:
42: # Loop through all the zip files
43:
44: if ($scope =~ /(zip)|(all)/ ) {
45:
46: while (defined ($file = glob("*.zip"))) {
47:
48: print "$file: ";
49:
50: $zip = Archive::Zip->new();
51: die 'Bad zip file!' if $zip->read( $file ) != AZ_OK;
52:
53: # We only expect one member/file [for now]
54: @members = $zip->memberNames();
55: $extracted = $members[0] . ".tmp";
56: die "could not extract $members[0]!" if $zip->extractMember($members[0], $extracted) != AZ_OK;
57:
58: print "Extracted $members[0], ";
59:
60: # Now we've got $file.log.tmp
61:
62: # Find string, write to file
63: open(INFILE, $extracted);
64: print "finding matches, ";
65:
66: while(<INFILE>){
67: if (/$phrase/) {
68: print OUTFILE "$_";
69: }
70: }
71:
72: close(INFILE);
73:
74: $result = `del $extracted`;
75:
76: print" done.\n\n";
77: }
78: }
79:
80: # Loop through .log files
81:
82: if ($scope =~ /(log)|(all)/ ) {
83:
84: while (defined ($file = glob("*.log"))) {
85:
86: print "$file: ";
87:
88: # Don't have to extract the file here, skip right to searching
89:
90: open(INFILE, $file);
91: print "finding matches, ";
92:
93: while(<INFILE>){
94: if (/$phrase/) {
95: print OUTFILE "$_";
96: }
97: }
98:
99: close(INFILE);
100:
101: print" done.\n\n";
102: }
103: }
104:
105: # Change back to starting directory
106: chdir $cwd;
107:
108: # Close file
109:
110: close(OUTFILE);
111:
112: sub usage {
113: print ("search.pl -- Find lines with a given phrase from a directory of logfiles.\n");
114: print ("useage: search.pl [zip|log|all] [path] [phrase] [outfile]\n\n");
115: print ("option 'zip' will strip compessed .zip archives\n");
116: print ("option 'log' will strip uncompressed .log files\n");
117: print ("option 'all' will do both .zip and .log files\n");
118: print ("[path] should be a full-length path surrounded by double-quotes.\n");
119: exit(0);
120:
121: }
122:
123:
124: /rgds,
125: ibanix
In reply to compressed logfile grep by ibanix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |