The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can we call JSP file from Unix.if so how.Please help me.Im newbie to Unix mailsukumar Shell Programming and Scripting 0 05-26-2008 05:12 AM
UNIX newbie NEWBIE question! Hanamachi UNIX for Dummies Questions & Answers 3 09-14-2006 07:23 AM
hello unix newbie Combat Form UNIX for Dummies Questions & Answers 1 05-01-2004 05:16 AM
Unix Newbie u6ik UNIX Desktop for Dummies Questions & Answers 7 03-19-2004 08:11 AM
am a unix newbie mysticalpotato UNIX for Dummies Questions & Answers 12 09-15-2003 09:05 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-21-2007
Registered User
 

Join Date: Feb 2007
Posts: 1
Stumble this Post!
Newbie to HP Unix

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

thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-21-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
Try something like this:
Code:
$ 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.
Reply With Quote
  #3 (permalink)  
Old 02-22-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
A version a little more concise of Perderabo script :
Code:
#!/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

Jean-Pierre~
Reply With Quote
  #4 (permalink)  
Old 02-22-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
Quote:
Originally Posted by aigles
A version a little more concise of Perderabo script :
[code]timetosecs()
{
integer time h m s
echo $1 | IFS=: read h m s
echo $((h*3600+m*60+s))
}
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0