Hi
Im a newbie to unix having been a windows guy, yes I know a swear word on here ? but part of my new job is to be trained up on Unix.
On the course we were set a task to read a file that has dates times codes etc, a log file. see below as an example
2007/02/19 00:00:06: Information: Switch SDT for out stream 510
2007/02/19 01:00:00: Information: Switch SDT for out stream 510
2007/02/19 01:59:00: Information: Switch SDT for out stream 510
My task is to look through this file and display the output for a start time and end time ?
so eg start time is 01:00:00 and end time 05:00:00
My question is how do you do this and what if the first time is 01:01:00, how do you search for this ?
My code so far is this
#! /bin/sh
rm temp.log
clear
echo "Please enter word to search for : "
read searchstring
echo "Please enter outstream to search for : "
read outstream
echo "Switch or Build : "
read switchorread
echo "Please enter search date : (yyyy/mm/dd) "
read searchdate
echo "Please enter start time : (hh:mm:ss) "
read starttime
echo "Please enter end time : (hh:mm:ss) "
read endtime
echo $searchstring $outstream $switchorread $searchdate $starttime $endtime
cat /users/duncan/shepd/xsg.log|grep -i $searchstring|grep $outstream|grep -i $switchorread|grep $searchdate >temp.log
The expamle at the top is the out put for temp.log
How can I search temp.log for a set time and end time and out put this this to screen or a file ?
Any help in point me to the right commands, rather then telling me what code I should write, Id appricate it
$ cat data
2007/02/19 00:00:06: Information: Switch SDT for out stream 510
2007/02/18 00:00:06: Information: Switch SDT for out stream 510
2007/02/19 01:00:00: Information: Switch SDT for out stream 510
2007/02/18 01:00:00: Information: Switch SDT for out stream 510
2007/02/19 01:59:00: Information: Switch SDT for out stream 510
2007/02/18 01:59:00: Information: Switch SDT for out stream 510
$
$
$ cat script
#! /usr/bin/ksh
timetosecs()
{
typeset time h m s
time=$1
time=${time%:}
h=${time%%:*}
time=${time#${h}:}
m=${time%:*}
s=${time#*:}
h=${h#0}
m=${m#0}
s=${s#0}
echo $((h*3600+m*60+s))
}
#clear
echo "Please enter word to search for : \c"
read searchstring
echo "Please enter outstream to search for : \c"
read outstream
echo "Switch or Build : \c"
read switchorread
echo "Please enter search date : (yyyy/mm/dd) \c"
read searchdate
echo "Please enter start time : (hh:mm:ss) \c"
read starttime
startsecs=$(timetosecs $starttime)
echo "Please enter end time : (hh:mm:ss) \c"
read endtime
endsecs=$(timetosecs $endtime)
exec < $outstream
while read date time rest ; do
[[ $date != $searchdate ]] && continue
secs=$(timetosecs $time)
((secs<startsecs)) && continue
((secs>endsecs)) && continue
echo $date $time $rest
done
$
$
$ ./script
Please enter word to search for : sjsjs
Please enter outstream to search for : data
Switch or Build : sjsjs
Please enter search date : (yyyy/mm/dd) 2007/02/19
Please enter start time : (hh:mm:ss) 01:00:00
Please enter end time : (hh:mm:ss) 02:00:00
2007/02/19 01:00:00: Information: Switch SDT for out stream 510
2007/02/19 01:59:00: Information: Switch SDT for out stream 510
$
The secret is converting time to seconds after midnight for easy comparing.
A version a little more concise of Perderabo script :
#!/usr/bin/ksh
timetosecs()
{
integer time h m s
echo $1 | IFS=: read h m s
echo $((h*3600+m*60+s))
}
read searchstring?"Please enter word to search for : "
read outstream?"Please enter outstream to search for : "
read switchorread?"Switch or Build : "
read searchdate?"Please enter search date : (yyyy/mm/dd) "
read starttime?"Please enter start time : (hh:mm:ss) "
read endtime?"Please enter end time : (hh:mm:ss) "
startsecs=$(timetosecs $starttime)
endsecs=$(timetosecs $endtime)
exec < $outstream
while read date time rest ; do
[[ $date != $searchdate ]] && continue
secs=$(timetosecs $time)
(( secs < startsecs && secs > endsecs )) && continue
echo $date $time $rest
done
That is a bit too concise. Posix has mandated that an integer which starts with a leading zero must be treated as an octal number. That means that attempting arithmetic with an integer like 09 is not legal and it will fail with many versions of ksh including the ksh supplied with recent versions of HP-UX.
You need:
h=${h#0}
m=${m#0}
s=${s#0}
I also think that limiting the scope of h, m, and s is a good idea, but using global variables causes no harm in this script as it currently exists.