Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, Here is the senerio, We logged the processing and transfer of emails on our exchange server. But recently the logs are so huge (300MB and above) that there is no way of opening it to find a certain text, like searching for a line of code. Is there a way (Like grep in Unix) to search for a certain text in the log file(ex: Perl Search.pl JohnDoe@test.com), maybe passed the exact text to the script to search and print out the result via HTML or generate a seperate log file with the result content? Thank you so much!

Replies are listed 'Best First'.
Re: Matching text in Perl
by Aristotle (Chancellor) on Jul 23, 2002 at 22:15 UTC
Re: Matching text in Perl
by BrowserUk (Patriarch) on Jul 24, 2002 at 00:31 UTC

    I know I am gonna get slaughtered for this but...you don't actually need to code a Perl solution, nor install any extra software to do this...

    C:\test>help findstr Searches for strings in files. FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:fil +e] [/C:string] [/G:file] [strings] [[drive:][path]filename[ ...]] /B Matches pattern if at the beginning of a line. /E Matches pattern if at the end of a line. /L Uses search strings literally. /R Uses search strings as regular expressions. /S Searches for matching files in the current directory and a +ll subdirectories. /I Specifies that the search is not to be case-sensitive. /X Prints lines that match exactly. /V Prints only lines that do not contain a match. /N Prints the line number before each line that matches. /M Prints only the filename if a file contains a match. /O Prints character offset before each matching line. /P Skip files with non-printable characters /F:file Reads file list from the specified file(/ stands for conso +le). /C:string Uses specified string as a literal search string. /G:file Gets search strings from the specified file(/ stands for c +onsole). strings Text to be searched for. [drive:][path]filename Specifies a file or files to search. Use spaces to separate multiple search strings unless the argument is +prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello +" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y. For information on FINDSTR regular expressions refer to the online Com +mand Reference.

    Of course, if you want to do much more than just finding the references, you will probably want to use Perl anyway.

Re: Matching text in Perl
by tadman (Prior) on Jul 23, 2002 at 22:13 UTC
    Here's a simple example which gives you a grep-like framework:
    #!/usr/bin/perl -w use strict; my $logfile = "/.../access_log"; open(LOG, $logfile) || die "Could not read $logfile\n"; while (<LOG>) { next unless (/\Q$ARGV[0]\E/); print; } close(LOG);
    Note that if you want to print out the result as HTML, you'll have to parse the log file. If you're using Apache, there are a few tools which can make this much simpler, such as Log::Parser a.k.a. Apache::ParseLog.
Re: Matching text in Perl
by jmcnamara (Monsignor) on Jul 23, 2002 at 22:19 UTC

    You could try something like this (Dos style shell quotes):    perl -ne "BEGIN{$pattern = shift} print if /\Q$pattern\E/o" pattern file.log

    But you should really install a Win32 binary of gnu grep.

    --
    John.

A reply falls below the community's threshold of quality. You may see it by logging in.