In bash I am trying to read a log file and will print only the lines that have a timestamp between two specific times. The time format is hh:mm:ss. For example, I would be searching for lines that would fall between 12:52:33 to 12:59:33.
I want to use regular expression because I can use it in grep
function.
Each log line begins with some_nr 2014-05-15 21:58:00,000000 rest_of_line
.
My solution gives me lines with 1 min margin. I cut out ss
and take all lines with hh:mm:[0-9]{2}
. $2 has format filename_hh:mm:;
for example: "24249_16:05:;24249_16:05:;24249_16:07:;24249_16:07:;24249_16:08:"
My code:
B=$2
for line in ${B//;/ } ;
do
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 a solution with 15 sec margin for any time not 60. I want to have input in format filename_hh:mm:ss
and take lines for hh:mm:ss +/- 15s or filename_hh:mm:ss(1)_hh:mm:ss(2)
and take lines between hh:mm:ss(1) and hh:mm:ss(2). For sometime there is no lines so the solution should 'recognize' if sometimes match inputted interval or not.
Log files look like 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 a bla
1001667 1001667 2014/15/05 22:09:44.592009 bl a bla
I believe sed is the 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: Get 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 to inactivity
log file is usually sorted by timestamp, assume the timestamp is on the first column, you could:
awk -v from="12:52:33" -v to="12:59:33" '$1>=from && $1<=to' foo.log
in this way, you can change the from and to
to get different set of log entries. regex is not a good tool to do number calculation/comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With