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

Monks of wisdom,

I have 2 scripts both are functional however I want to integrate the 2 into 1. I want the grep script to run inside the timesearch script. Both of the scripts follow below. I've tried to integrate them and almost had it. It would return a result of what I wanted but only one and not the rest of them. Here are the 2 scripts I mentioned below. The first script is the "grep" script.

#!/usr/bin/perl #This script will "grep" for msisdn's originating and destination and +transaction numbers for the MXE LOGS or#"mon402.log" chomp($key = <STDIN>); open(FILE, "mon402.log"); while (<FILE>) { foreach (sort {$a <=> $b} grep /$key/, grep s/<(.*?)>/TEXT/, $_) { print $_; } }
The script below is the timesearch script -
#!/usr/bin/perl -w use strict; my %mylist; my $min; my $max; my $range; my $line; # get the range (in this throwtogether it must be entered # with no spaces in the form HH:MM:SS.TS-HH:MM:SS.TS) # Get the range $range = <STDIN>; # Break up the range ($min, $max) = split /-/, $range; # Squeeze out leading and trailing spaces $min =~ s/^\s+//; $min =~ s/\s+$//; $max =~ s/^\s+//; $max =~ s/\s+$//; # Open the datafile and break into fields open(FILE, "mon402.log"); while (<FILE>) { if (s/<(.*?)>/TEXT/) { if ($_ =~ m/(\d+\-\w+\-\d+)\s(\d{2}\:\d{2}\:\d{2}\.\d{2})\s+(\ +w*)/) { # Push the restricted range of filenames onto a hash of ar +rays # keyed on the time field if (($2 ge $min) && ($2 le $max)) { push(@{$mylist{$2}}, $_); } } } } my @keys = sort (keys %mylist); foreach my $key (@keys) { foreach my $thing (@{%mylist}{$key}){ foreach my $it (@$thing) { print "$it\n"; last; } } }
Now they are separate scripts but I need them to be one and produce the correct results. Now the data that is being read is a datalog. The lines look like the following.
27-Jun-2001 11:34:15.77 SendSMReq:T:1074 D:19209131848 O:19209131065 +P:0x20 C:0x0 V:07-05 11:29 TEXT 27-Jun-2001 11:36:06.76 SendSMReq:T:1074 D:19207120303 O:19207131623 +P:0x20 C:0x0 V:07-05 11:35 TEXT 27-Jun-2001 11:37:51.56 SendSMReq:T:1074 D:19209138833 O:19202928969 +P:0x20 C:0x0 V:06-27 12:55 TEXT 27-Jun-2001 11:39:23.19 SendSMReq:T:1074 D:19207121543 O:Voice Mail M +WI P:0x20 C:0xe1 V:07-05 11:39 TEXT 27-Jun-2001 11:41:08.90 SendSMReq:T:1074 D:16084468213 O:16083082487 +P:0x20 C:0x0 V:06-27 12:40 TEXT
Now the timesearch script finds lines that are inbetween a timerange I specify. The grep script will grep on pretty much anything I give it. Data I would give the timesearch script is obvious but for the grep let's say I wanted to find transaction# 1074 which is (if you look in the data) right after time. Now let's say I ran the timesearch script with the grep script inside. I would get only one line back.

27-Jun-2001 11:39:23.19 SendSMReq:T:1074 D:19207121543 O:Voice Mail MWI P:0x20 C:0xe1 V:07-05 11:39 TEXT

and you can see from the data more than one for trans# 1074 exist.
So I'm trying to make the 2 work together in one script while getting the correct data output. This is giving me a big stress workout so I'd love it if somebody could help me out here.

The brassmonk
May the force be with me.

Replies are listed 'Best First'.
Re: grep & timesearch
by bikeNomad (Priest) on Jul 12, 2001 at 06:33 UTC
    It's not clear what you mean by "let's say I ran the timesearch script with the grep script inside". But you have two conditions here: the regex match and the time range. You can just combine them:

    if (($2 ge $min) && ($2 le $max) && /$key/) { push(@{$mylist{$2}}, $_); }
      Yes that would work it would just be like using perls grep function However I tried what you suggested and even put $key in the split function line of the timesearch script and it won't find what I search on and I know the script should find data because I verified the data to match. I even expanded my pattern match line so I could $key what field to look in and what it should be equal to but that didn't work either. For example here's what the lines of code I changed to try to make it work. Of course with $range equaling <STDIN>.
      ($min, $max, $key) = split /-/, $range;
      if (($2 ge $min) && ($2 le $max) && ($3 eq $key)) { 
            push(@{$mylist{$2}}, $_); 
        }
      
      I ALSO TRIED THE NEXT CODE
      ($min, $max, $key) = split /-/, $range;
      if (($2 ge $min) && ($2 le $max) && /$key/) { 
            push(@{$mylist{$2}}, $_);
      }
      
      I ALSO TRIED THIS CODE BELOW TOO
      $range = <STDIN>;
      $key = <STDIN>;
      ($min, $max) = split /-/, $range;
      if (($2 ge $min) && ($2 le $max) && grep /$key/, $_) { 
            push(@{$mylist{$2}}, $_);
      }
      
      Excuse my dumbness but I can't figure it out.

      The brassmonk