Solved Count Entries based on time ranges

November 6, 2012 at 05:56:18
Specs: Solaris
Hi Experts,

I am doing 2 scripts using awk, 1) to count entries that fall into certain time ranges and 2) separate the content of the file into different files:

## INPUT
--------------------------------------------------------------------------
Sun Sep 09 02:02:03 2012
Stop
PPP
IP
PortDetail

Sun Sep 09 02:29:03 2012
Alive
Framed
MAC

Sun Sep 09 02:31:03 2012
Stop
Framed
PPP

Sun Sep 09 14:02:03 2012
Stop
Framed
PPP
MAC
--------------------------------------------------------------------------

## OUTPUT for 1st Script
--------------------------------------------------------------------------
Count for 00:00 to 02:30 time range - 2
Count for 02:31 to 23:59 time range - 2
--------------------------------------------------------------------------


## OUTPUT for the 2nd script
--------------------------------------------------------------------------

cat Entries_0000TO0230

Sun Sep 09 02:02:03 2012
Stop
PPP
IP
PortDetail

Sun Sep 09 02:29:03 2012
Alive
Framed
MAC


cat Entries_0231TO2359

Sun Sep 09 02:31:03 2012
Stop
Framed
PPP

Sun Sep 09 14:02:03 2012
Stop
Framed
PPP
MAC
--------------------------------------------------------------------------


Can these be achieved using awk? Any input is highly appreciated.


Best Regards,
Amiti


See More: Count Entries based on time ranges

Report •

#1
November 7, 2012 at 20:59:06
✔ Best Answer
I'll do the first script for you. Let me know if you have any questions

#!/bin/ksh

awk ' {  # use nawk for solaris
if(match($4,":") )
  {
  gsub(":", "", $4)
  x=substr($4, 1, length($4) - 2) # get the first 6 characters
  y=x+0 # make a string an integer

  if(y >= 0 && y <= 230)
     arr[0]++

  if(y >= 231 && y <= 2359)
     arr[231]++

  }
}

END { for (ind in arr)
         {
         if(ind == 0)
            print "Count for 00:00 to 02:30 time range - " arr[0]

         if(ind == 231)
            print "Count for 02:31 to 23:59 time range - " arr[231]
         }
    }
' input.txt


Report •

#2
November 8, 2012 at 19:22:30
HI Nails,

Thank you very much for your usual support. I am able to achieve what I want for script 1.

Best Regards,
Amiti


Report •
Related Solutions


Ask Question