Count Número de linhas o começo com data e data e hora

0

Como posso contar o número de linhas que começam com o carimbo de data e hora que corresponde a uma hora ou menos a partir de agora?

O arquivo neste local /home/liamcomfort/my.log .

Um exemplo do formato do log é:

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
    
por liamjeanius 15.11.2013 / 20:42

2 respostas

1

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
    
por glenn jackman 16.11.2013 / 22:43
1
while read M D T R; do [[ $(( $( date +"%s" ) - $( date -d "$M $D $T" +"%s" ) )) -lt 3600 ]] && (( ++C )); done; echo $C

Nice oneliner (desculpe, não resisti) que funciona mas muito feia ...
O seguinte código, no entanto, é como isso pode ser feito de uma maneira elegante:

#!/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"
    
por thom 16.11.2013 / 23:14