Re: Grep through a log file



Matt,

A couple of things here. first, you don't perform any modification of
$culist, but the strings in $culist don't appear unmodified in the log
file. the string perl reads into $_ from a file like you're example is
e.g. "SUN9-GT:\n". The string in the log file, though, is just
"SUN9-GT". try something like '$culist =~ tr/:\n//' or

while (my $culist = <CULIST>) {
chomp $culist;
$culist =~ s/(\w+):/$1/;
# .... the rest of your code
}

Next, grep(). grep takes a list and returns the elements of the list
that match. the 'while (<>)' construct, though, only reads a line at
time, and you don't seem to be splitting into an array. That makes
grep superfluous. All you need here is:

while (<SEPTEMBER>) {
chomp;
print CU_OUTPUT "$_\n\n" if /$culist/;
print "$_\n\n";
}


HTH,

-- jay

First, thanks for your input Jay, Rob, Lawrence

Jay,

I tried your script. What happens is it gets through the first
iteration of the script and copies the appropriate lines to the new log
file. But each subsequent iteration is skipped over.

This is what I have:


use warnings;
use strict;
open CULIST, "/root/syslog_stuff/CULIST3.txt" or die $!;

open SEPTEMBER, "/var/log/log_netscreen_sep" or die $!;

while (<CULIST>) {
chomp (my $culist = $_);
print "$culist\n";
open CU_OUTPUT,">> /root/syslog_stuff/monthly_logs/sep2/$culist"or die
$!;

while (<SEPTEMBER>) {
chomp;
print CU_OUTPUT "$_\n\n" if /$culist/;
# print "$_\n\n";
}

}

The script goes through each $culist (shown by printing the each
variable) - but it looks like it's skipping over the "while
(<SEPTEMBER>)" part for the rest of the $culist. Any ideas?

Matt

.



Relevant Pages

  • Re: searching for text strings(2) in filenames: with command possible or script needed?
    ... > I would like to be able to search for 2 different strings in filenames ... > or is it neccessary to write a script? ... ...if you want to have grep search for multiple patterns, ...
    (comp.os.linux.misc)
  • Re: opening the file at a specific line number.
    ... log file. ... If you really mean simple grep'ing for strings then I wonder if it might be easier to write it as a shell script. ... Each time you grep write the number of lines to a file and then when you wake up the next time do a bit of arithmetic with the current state of the file and the previous size in "transient_file" to give a starting line number. ...
    (comp.lang.perl.misc)
  • Help: Match Error
    ... Here's my codes to match the following strings: ... when I run my script it outputs nothing. ...
    (comp.lang.perl.misc)
  • Re: while (1) vs. for ( ;; )
    ... > To bad grep doesn't know how to ignore C comments and literals. ... or is this just a silly objection for the ... Use an editor and fix the problem. ... In cases where 'while' appears inside strings (or ...
    (comp.lang.c)
  • Where are the strings in gc.get_objects?
    ... script to show the numbers of each different type of object. ... for key in keys: ... I get similar results on both Python 2.4 and Python 2.5. ... Can anyone explain were the strings are? ...
    (comp.lang.python)