grep time and date

Hi,

I have a file which is a result of a script running every two minutes. What I wanted to do is to grep a specific date and time (hour and minute) from the file and then count the occurance of 201. I need to get the result of occurance of 201 every 5 minutes. What should I include in my command below to do that? I know I need to create variables for the date, just don't know how to do it. Thanks in advance! I hope someone can help me.

cat result.txt | nawk '{print $9}' | grep "201" | wc -l

Via NODE 4 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60232010 bbbbbbbb0232010 0.0 -1 0.0 0 0 
Via NODE 0 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa99461665 bbbbbbbb65      0.0 -1 0.0 0 0 
Via NODE 7 CP2 20071217 17:59:54 20071217 17:59:59 201 103 644 aaaaaaaa62179668 bbbbbbbb2179668 0.0 -1 0.0 0 0 
Via NODE 7 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60239184 bbbbbbbb0239184 0.0 -1 0.0 0 0 
Via NODE 0 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60230772 bbbbbbbb0230772 0.0 -1 0.0 0 0 
Via NODE 2 CP2 20071217 17:59:54 20071217 17:59:59 201 103 630 aaaaaaaa60252750 bbbbbbbb0252750 0.0 -1 0.0 0 0 

try this:

cat result.txt | grep " 17:59:" | grep -c " 201 "

You don't need so many different 'piped' tools (let alone UUoC)
Not sure I fully understand the requirement......
What's your input? Date? Or data AND time (hour/minute)?
Why do you need 'hour/minute' if you if want to stat count for every FIVE minutes worth of data?
If would help if you could provide

  1. a better/respresentative data sample
  2. what the expected input parameter(s) supposed to be
  3. what the desired output supposed to be

or better yet:

cat result.txt | egrep " 17:(55|56|57|58|59):" | grep -c " 201 "

Hi,

Thanks for the response. What I need to do is to get the number of occurance of "201" every 5 minutes based on the time from the result.txt file. For example:

Output:

2007-12-18_17:59: to 18:04 201Count=301
2007-12-18_18:05: to 18:09 201Count=100
2007-12-18_18:10: to 18:14 201Count=798

and so on...

vgersh99 was saying about the UUOC

you don't need a cat there.

Hi Matrixmadhan,

It's you again and it's me again! :slight_smile: I don't need a specific date and time. What I need to do is to count the number of occurance of 201 every interval of 5 minutes continously. The result.txt file is continously being appended by data like the sample below. I hope you can help me.

contents of result.txt:
Via NODE 4 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60232010 bbbbbbbb0232010 0.0 -1 0.0 0 0
Via NODE 0 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa99461665 bbbbbbbb65 0.0 -1 0.0 0 0
Via NODE 7 CP2 20071217 17:59:54 20071217 17:59:59 201 103 644 aaaaaaaa62179668 bbbbbbbb2179668 0.0 -1 0.0 0 0
Via NODE 7 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60239184 bbbbbbbb0239184 0.0 -1 0.0 0 0
Via NODE 0 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60230772 bbbbbbbb0230772 0.0 -1 0.0 0 0
Via NODE 2 CP2 20071217 18:01:54 20071217 17:59:59 201 103 630 aaaaaaaa60252750 bbbbbbbb0252750 0.0 -1 0.0 0 0
Via NODE 4 CP2 20071217 18:01:53 20071217 17:59:59 201 103 630 aaaaaaaa60232010 bbbbbbbb0232010 0.0 -1 0.0 0 0
Via NODE 0 CP2 20071217 18:02:53 20071217 17:59:59 201 103 630 aaaaaaaa99461665 bbbbbbbb65 0.0 -1 0.0 0 0
Via NODE 7 CP2 20071217 18:04 20071217 17:59:59 201 103 644 aaaaaaaa62179668 bbbbbbbb2179668 0.0 -1 0.0 0 0
Via NODE 7 CP2 20071217 18:05:53 20071217 17:59:59 201 103 630 aaaaaaaa60239184 bbbbbbbb0239184 0.0 -1 0.0 0 0
Via NODE 0 CP2 20071217 18:08:53 20071217 17:59:59 201 103 630 aaaaaaaa60230772 bbbbbbbb0230772 0.0 -1 0.0 0 0
Via NODE 2 CP2 20071217 18:09:54 20071217 17:59:59 201 103 630 aaaaaaaa60252750 bbbbbbbb0252750 0.0 -1 0.0 0 0

Hi Matrixmadhan,

It's you again and it's me again! :slight_smile: I don't need a specific date and time. What I need to do is to count the number of occurance of 201 every interval of 5 minutes continously. The result.txt file is continously being appended by data like the sample below. I hope you can help me.

Via NODE 4 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60232010 bbbbbbbb0232010 0.0 -1 0.0 0 0 
Via NODE 0 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa99461665 bbbbbbbb65      0.0 -1 0.0 0 0 
Via NODE 7 CP2 20071217 17:59:54 20071217 17:59:59 201 103 644 aaaaaaaa62179668 bbbbbbbb2179668 0.0 -1 0.0 0 0 
Via NODE 7 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60239184 bbbbbbbb0239184 0.0 -1 0.0 0 0 
Via NODE 0 CP2 20071217 17:59:53 20071217 17:59:59 201 103 630 aaaaaaaa60230772 bbbbbbbb0230772 0.0 -1 0.0 0 0 
Via NODE 2 CP2 20071217 18:01:54 20071217 17:59:59 201 103 630 aaaaaaaa60252750 bbbbbbbb0252750 0.0 -1 0.0 0 0 
Via NODE 4 CP2 20071217 18:01:53 20071217 17:59:59 201 103 630 aaaaaaaa60232010 bbbbbbbb0232010 0.0 -1 0.0 0 0 
Via NODE 0 CP2 20071217 18:02:53 20071217 17:59:59 201 103 630 aaaaaaaa99461665 bbbbbbbb65      0.0 -1 0.0 0 0 
Via NODE 7 CP2 20071217 18:04 20071217 17:59:59 201 103 644 aaaaaaaa62179668 bbbbbbbb2179668 0.0 -1 0.0 0 0 
Via NODE 7 CP2 20071217 18:05:53 20071217 17:59:59 201 103 630 aaaaaaaa60239184 bbbbbbbb0239184 0.0 -1 0.0 0 0 
Via NODE 0 CP2 20071217 18:08:53 20071217 17:59:59 201 103 630 aaaaaaaa60230772 bbbbbbbb0230772 0.0 -1 0.0 0 0 
Via NODE 2 CP2 20071217 18:09:54 20071217 17:59:59 201 103 630 aaaaaaaa60252750 bbbbbbbb0252750 0.0 -1 0.0 0 0

Expected Output:

2007-12-18_17:59: to 18:04 201Count=301
2007-12-18_18:05: to 18:09 201Count=100
2007-12-18_18:10: to 18:14 201Count=798
and so on...

:slight_smile: :slight_smile:

Assuming the data 201 is at field no : 8 ( starting from 0 )

Give this a try !

#! /opt/third-party/bin/perl

open(FILE, "<", "a");

while(<FILE>) {
  chomp;
  my @arr = split(/ /);
  next if( $arr[8] !~ /201/ );
  my ($nh, $nm, $ns) = split(/:/, $arr[7]);
  my ($ch, $cm, $cs) = split(/:/, $arr[5]);
  if( $cm + 5 >= 60 ) {
    $cm = 4;
    $ch + 1 >= 24  ? $ch = 0 : $ch++;
  }
  else {
    $cm+=5;
  }
  $fileHash{"$nh:$nm:$ns-$ch:$cm:$cs"}++;
}

close(FILE);

foreach my $k ( sort keys %fileHash ) {
  print "key:$k val:$fileHash{$k}\n";
}

exit 0

Hi Matrixmadhan,

Happy new year! Sorry for the late reply! My script is working now. Thanks a lot for all your patience and help! :slight_smile: