Ubuntu: Count Number of Lines the begin with date and timestamp


How can I count the number of lines that begin with the date and time stamp that matches one hour or less from now?

The file in this location /home/liamcomfort/my.log.

An example of the log's format is:

Nov 10 04:03:00 Friendly. Wholesale sweaters, no problems, sometimes overnight, inexpensive than large element.   Nov 10 04:03:07 Now want to grab the spotlight form the official website.   Nov 10 04:04:01 No matter who, overall planning, implementation than electric. I will be traveling to Japan tomorrow, at best.   Nov 10 04:04:01 Get's not, the element of which that can be used, unless the slough, the bow to the policies, comprehensive package  


now=$( date +%s )  one_hour_ago=$( date -d "now - 1 hour" +%s )  count=0  while read -ra words; do      timestamp=$( date -d "${words[@]:0:3}" +%s )      if (( $one_hour_ago <= $timestamp && $timestamp <= $now )); then          (( count ++ ))      fi  done < filename  echo $count  


while read M D T R; do [[ $(( $( date +"%s" ) - $( date -d "$M $D $T" +"%s" ) )) -lt 3600 ]] && (( ++C )); done; echo $C  

Nice oneliner (sorry, couldn't resist) which works but very very ugly...
The following code, however, is how it can be done in a elegant way:

#!/bin/bash    #read the current date/time in unixtime format  NOW=$( date +"%s" )    while read MONTH DAY TIME REST  do      #change the read date/time to unixtime      THEN=$( date -d "$MONTH $DAY $TIME" +"%s" )        #if the difference is less than 1 hour (=3600 seconds), increase the count      (( ( NOW - THEN ) < 3600 )) && (( ++COUNT ))    done <"/home/liamcomfort/my.log"    echo "$COUNT"  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »