ÈÐ Wïgê† Ðð† ñåmê

ïñ£ð§ê¢, ïñ£ðrmå†ïðñ §ê¢µr, Ðïgï†ål £ðrêñ§ï¢§, hå¢kïñg, §¥§†êm åÐmïñ阮rå†ïðñ, lïñµx ßlðg


I’ve wrote about this before in Using Sed to search between dates and offered a ad-hoc solution but the other day I came up with a much better solution using a little known option of ‘date’ command.  Using this new method, you just pass the time in minutes prior to current time.  I.e. if you want the last hour, you would simply type ‘./sed_time.sh 60’ and it will spit out the correctly formatted sed command like this:

[codesyntax lang=”bash”]

$ ./sed_time.sh 60
sed -n '/01\/May\/2017\:07\:16\:15/,/01\/May\/2017\:08\:16\:15/ p'

[/codesyntax]

So here is the new script … if you want to see the previous way, check the article above.

I currently use it to review nginx logs so the time format is in that format.  Also, this is helpful if you have large nginx logs and don’t want to load all of it in your favorite editor when you only need to look at a few hundred lines:

[codesyntax lang=”bash”]

#!/bin/bash
#===============================================================================
#
#          FILE:  sed_time.sh
# 
#         USAGE:  ./sed_time.sh {time_in_minutes} 
# 
#   DESCRIPTION:  pass an option of time in minutes prior to current time and this
#                 script will give you the sed format to extract logs from a file
#                 between the two dates/times
#       OPTIONS:  time in minutes, i.e. 60 would be 1 hour prior to current time
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Ed Wiget (), [email protected]
#       COMPANY:  Somewhere
#       VERSION:  1.0
#       CREATED:  04/28/2017 11:25:39 AM EDT
#      REVISION:  ---
#===============================================================================

if [ "$1" = "" ]; then
	echo "how long ago as starting time in minutes, i.e. 30 for 30 minutes"
	read PTIME
else
	PTIME=$1
fi

# current time
DNOW=`date +%d/%b/%Y:%H:%M:%S`

# time formatted escaped sed ready
ESCAPED_DNOW=`echo ${DNOW} | sed 's,/,\\\/,g' | sed 's,:,\\\:,g'`

# previous time
PDATE=`date +%d/%b/%Y:%H:%M:%S --date="${PTIME} minutes ago"`

# time formatted escaped sed ready
ESCAPED_PDATE=`echo ${PDATE} | sed 's,/,\\\/,g' | sed 's,:,\\\:,g'`

# now we can simply echo the sed line
echo "sed -n '/${ESCAPED_PDATE}/,/${ESCAPED_DNOW}/ p'"

[/codesyntax]


Leave a Reply