Extract log efficiently by using sed

Modification of Log pulling command by using sed

Old command That I use to do log extraction

sed -nre '/12:23:12/,/12:24:12/ p' logfile > /tmp/somelog.txt

But this will take match of given two time stamps at any where in log file.And sometimes from middle of the line which gives obviously wrong log that I need.

So , Now we are modifying sed to look after only at lines starting with timestamp.
It will pull the logs if timestamp exists at beginning of the lines else It wont pull any log.

sed -nre '/^12:23:12/,/^12:24:12/ p' logfile > /tmp/somelog.txt

Note: If you want to make sure that given timestamp existed or not at line starting
you can use

grep -m 5 12:23:12 server.log

The above command ran for 5 first matching of grep and so you can conclude the availability of
given time-stamp in logfile.

For more information please refer sed man page

Hope it will help you.


0 Comments