How to search for lines in a file between two timestamps using Bash -


in bash trying read log file , print lines have timestamp between 2 specific times. time format hh:mm:ss. example, searching lines fall between 12:52:33 12:59:33.

i want use regular expression becouse can use in grep function.

each logline begins some_nr 2014-05-15 21:58:00,000000 rest_of_line.

my solution gives me lines 1 min margin. cut out ss , take lines hh:mm:[0-9]{2}. $2 has format filename_hh:mm:; example: "24249_16:05:;24249_16:05:;24249_16:07:;24249_16:07:;24249_16:08:"

my code:

b=$2    line in ${b//;/ } ;       tent=`echo $line | awk '{split($0,numbers,"_"); print numbers[1]}'`"_logs.txt"     time=`echo $line | awk '{split($0,numbers,"_"); print numbers[2]}'`"[0-9]{2}"      grep -ie ${time} ${tent} >> ${file1} done 

i need solution 15 sec margin time not 60. want have input in format filename_hh:mm:ss , take lines hh:mm:ss +/- 15s or filename_hh:mm:ss(1)_hh:mm:ss(2) , take lines between hh:mm:ss(1) , hh:mm:ss(2). time there no lines solution should 'recognize' if match inputed interval or not.

log files this:

1002143 1002143 2014/15/05 22:09:52.937004 bla  1002130         2014/15/05 22:09:44.786002 bla bla 1001667         2014/15/05 22:09:44.592009 bl bla 1001667 1001667 2014/15/05 22:09:44.592009 bl bla 

i believe sed best option:

sed -rne '/<timestamp>/,/<timestamp>/ p' <file> 

ex:

 tiago@dell:~$ sed -rne '/08:17:38/,/08:24:36/ p' /var/log/syslog  may 16 08:17:38 dell aptdaemon.worker: info: processing transaction /org/debian/apt/transaction/08a244f7b8ce4fad9f6b304aca9eae7a may 16 08:17:50 dell aptdaemon.worker: info: finished transaction /org/debian/apt/transaction/08a244f7b8ce4fad9f6b304aca9eae7a may 16 08:18:50 dell aptdaemon.packagekit: info: initializing packagekit transaction may 16 08:18:50 dell aptdaemon.worker: info: simulating trans: /org/debian/apt/transaction/37c3ef54a6ba4933a561c49b3fac5f6e may 16 08:18:50 dell aptdaemon.worker: info: processing transaction /org/debian/apt/transaction/37c3ef54a6ba4933a561c49b3fac5f6e may 16 08:18:51 dell aptdaemon.packagekit: info: updates() may 16 08:18:52 dell aptdaemon.worker: info: finished transaction /org/debian/apt/transaction/37c3ef54a6ba4933a561c49b3fac5f6e may 16 08:24:36 dell aptdaemon: info: quitting due inactivity  

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -