From: Ed Morton on 13 Jan 2010 00:07 On 1/12/2010 10:36 PM, Icarus Sparry wrote: > On Tue, 12 Jan 2010 21:23:45 -0600, Ed Morton wrote: > >> On 1/12/2010 3:08 PM, Pankaj wrote: >>> On Jan 12, 3:54 pm, Ed Morton<mortons...(a)gmail.com> wrote: >>>> On Jan 12, 2:11 pm, Pankaj<harpreet.n...(a)gmail.com> wrote: >>>> >>>>> Greetings, >>>> >>>>> I have a file with following format >>>> >>>>> Task1 01/12/2010 14:14:04 >>>>> Task2 01/12/2010 14:00:07 >>>>> Task3 01/12/2010 14:00:42 >>>> >>>>> I am currently trying to achieve that if the time (last column) for >>>>> any of the above task is more than 2 hours (as compared to current >>>>> timing), then we need to get notified. >>>> >>>>> Can anyone advise how to go about it. I am currently using ksh >>>> >>>>> TIA >>>> >>>> This will print the number of hours difference and the task name for >>>> each line in your input file: >>>> >>>> gawk '{split($0,t,/[ /:]+/); >>>> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) / >>>> 3600, $1}' file >>>> >>>> Ed. >>> >>> Thanks Ed but it seems I do not have gawk in sh or ksh here. I get >>> following error >>> >>> gawk: not found >>> >>> Any alternate way to go about it. >> >> Just install gawk (http://www.gnu.org/software/gawk/), you'll thank >> yourself later... >> >> Ed. > > If you are using a reasonably recent ksh93, then you can ignore Ed's > advice as ksh93 can do it with builtin commands. I get where you're coming from, but just because you can apparently do this one thing with some constructs specific to some versions of ksh93 doesn't mean it's a bad idea to install and use gawk for this and your future text processing needs instead. Ed. The key is to note that > > printf "%(%#)T" now > > will give you the time in seconds since the epoch, and you can substitute > any reasonable phrase for "now", like "last monday". > > So something like > > while read task day time > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now) > if ((age> 2*3600)) > then printf "%s is %d seconds old\n" "$task" $age > fi > done > > should do it.
From: Pankaj on 13 Jan 2010 12:29 On Jan 13, 12:07 am, Ed Morton <mortons...(a)gmail.com> wrote: > On 1/12/2010 10:36 PM, Icarus Sparry wrote: > > > > > > > On Tue, 12 Jan 2010 21:23:45 -0600, Ed Morton wrote: > > >> On 1/12/2010 3:08 PM, Pankaj wrote: > >>> On Jan 12, 3:54 pm, Ed Morton<mortons...(a)gmail.com> wrote: > >>>> On Jan 12, 2:11 pm, Pankaj<harpreet.n...(a)gmail.com> wrote: > > >>>>> Greetings, > > >>>>> I have a file with following format > > >>>>> Task1 01/12/2010 14:14:04 > >>>>> Task2 01/12/2010 14:00:07 > >>>>> Task3 01/12/2010 14:00:42 > > >>>>> I am currently trying to achieve that if the time (last column) for > >>>>> any of the above task is more than 2 hours (as compared to current > >>>>> timing), then we need to get notified. > > >>>>> Can anyone advise how to go about it. I am currently using ksh > > >>>>> TIA > > >>>> This will print the number of hours difference and the task name for > >>>> each line in your input file: > > >>>> gawk '{split($0,t,/[ /:]+/); > >>>> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) / > >>>> 3600, $1}' file > > >>>> Ed. > > >>> Thanks Ed but it seems I do not have gawk in sh or ksh here. I get > >>> following error > > >>> gawk: not found > > >>> Any alternate way to go about it. > > >> Just install gawk (http://www.gnu.org/software/gawk/), you'll thank > >> yourself later... > > >> Ed. > > > If you are using a reasonably recent ksh93, then you can ignore Ed's > > advice as ksh93 can do it with builtin commands. > > I get where you're coming from, but just because you can apparently do this one > thing with some constructs specific to some versions of ksh93 doesn't mean it's > a bad idea to install and use gawk for this and your future text processing > needs instead. > > Ed. > > The key is to note that > > > > > > > printf "%(%#)T" now > > > will give you the time in seconds since the epoch, and you can substitute > > any reasonable phrase for "now", like "last monday". > > > So something like > > > while read task day time > > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now) > > if ((age> 2*3600)) > > then printf "%s is %d seconds old\n" "$task" $age > > fi > > done > > > should do it.- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text - Icarus: There seems to be some problem as I keep getting syntax error. Honestly, I truly did not understood how to above code works but just wanted to try to see if this resolves the issue This is what I tried #!/usr/bin/ksh while read task day time do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" "last monday") if ((age > 2*3600)) then printf "%s is %d seconds old\n" "$task" $age fi done < File1.dat test.ksh[3]: ()T-%#)T: syntax error Ed: To be honest, I truly dont think I can run or install anything in this environment until I have exhausted all other approaches.
From: Icarus Sparry on 13 Jan 2010 22:09 On Wed, 13 Jan 2010 09:29:47 -0800, Pankaj wrote: >> > If you are using a reasonably recent ksh93, then you can ignore Ed's >> > advice as ksh93 can do it with builtin commands. >> >> I get where you're coming from, but just because you can apparently do >> this one thing with some constructs specific to some versions of ksh93 >> doesn't mean it's a bad idea to install and use gawk for this and your >> future text processing needs instead. >> >> Ed. >> >> The key is to note that >> >> >> >> >> >> > printf "%(%#)T" now >> >> > will give you the time in seconds since the epoch, and you can >> > substitute any reasonable phrase for "now", like "last monday". >> >> > So something like >> >> > while read task day time >> > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now) >> > if ((age> 2*3600)) >> > then printf "%s is %d seconds old\n" "$task" $age fi >> > done >> >> > should do it.- Hide quoted text - >> >> - Show quoted text -- Hide quoted text - >> >> - Show quoted text - > > Icarus: There seems to be some problem as I keep getting syntax error. > Honestly, I truly did not understood how to above code works but just > wanted to try to see if this resolves the issue > > This is what I tried > > #!/usr/bin/ksh > > while read task day time > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" "last monday") > if ((age > 2*3600)) > then printf "%s is %d seconds old\n" "$task" $age fi > done < File1.dat > > test.ksh[3]: ()T-%#)T: syntax error What version of ksh are you running? You need a reasonably up-to-date ksh93. What does printf "%T\n" now say when you are running inside ksh? It should print out the date. What does echo "${.sh.version}" say, it should give something like Version JM 93t+ 2009-12-18 If there is nothing, or you get /usr/bin/ksh: ${.sh.version}: bad substitution then you are probably running ksh88 (and are probably on a sun). Often in that case you can get away with running /usr/dt/bin/dtksh, but unfortunately not in this case. dtksh is a ksh93 but not recent enough. So in this case you probably have to fall back to this http://groups.google.com/group/comp.unix.shell/msg/b17011dbda4bb76b and in particular the timegm function.
From: Pankaj on 14 Jan 2010 15:17 On Jan 13, 10:09 pm, Icarus Sparry <use...(a)icarus.freeuk.com> wrote: > On Wed, 13 Jan 2010 09:29:47 -0800, Pankaj wrote: > >> > If you are using a reasonably recent ksh93, then you can ignore Ed's > >> > advice as ksh93 can do it with builtin commands. > > >> I get where you're coming from, but just because you can apparently do > >> this one thing with some constructs specific to some versions of ksh93 > >> doesn't mean it's a bad idea to install and use gawk for this and your > >> future text processing needs instead. > > >> Ed. > > >> The key is to note that > > >> > printf "%(%#)T" now > > >> > will give you the time in seconds since the epoch, and you can > >> > substitute any reasonable phrase for "now", like "last monday". > > >> > So something like > > >> > while read task day time > >> > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now) > >> > if ((age> 2*3600)) > >> > then printf "%s is %d seconds old\n" "$task" $age fi > >> > done > > >> > should do it.- Hide quoted text - > > >> - Show quoted text -- Hide quoted text - > > >> - Show quoted text - > > > Icarus: There seems to be some problem as I keep getting syntax error. > > Honestly, I truly did not understood how to above code works but just > > wanted to try to see if this resolves the issue > > > This is what I tried > > > #!/usr/bin/ksh > > > while read task day time > > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" "last monday") > > if ((age > 2*3600)) > > then printf "%s is %d seconds old\n" "$task" $age fi > > done < File1.dat > > > test.ksh[3]: ()T-%#)T: syntax error > > What version of ksh are you running? You need a reasonably up-to-date > ksh93. What does > > printf "%T\n" now > > say when you are running inside ksh? It should print out the date. > What does > > echo "${.sh.version}" > > say, it should give something like > > Version JM 93t+ 2009-12-18 > > If there is nothing, or you get /usr/bin/ksh: ${.sh.version}: bad > substitution then you are probably running ksh88 (and are probably on a > sun). Often in that case you can get away with running /usr/dt/bin/dtksh, > but unfortunately not in this case. dtksh is a ksh93 but not recent > enough. > > So in this case you probably have to fall back to this > > http://groups.google.com/group/comp.unix.shell/msg/b17011dbda4bb76b > > and in particular the timegm function.- Hide quoted text - > > - Show quoted text - Thanks everyone. I was able to come up with following which is working pretty good. while read val1 val2 val3 do start=$val3 end=`date +%H:%M:%S` ((start_mins = $(expr substr "$start" 1 2)*60 + $(expr substr "$start" 4 2))) ((end_mins = $(expr substr "$end" 1 2)*60 + $(expr substr "$end" 4 2))) ((elapsed_mins = end_mins - start_mins)) if [[ "$elapsed_mins" -lt 0 ]] then ((elapsed_mins += 1440)) fi if [[ "$elapsed_mins" -gt 120 ]] then print $((elapsed_mins / 60)) hours and $((elapsed_mins % 60)) minutes fi done < file.txt
From: Edgardo Portal on 15 Jan 2010 10:29 On 2010-01-14, Pankaj <harpreet.noni(a)gmail.com> wrote: > On Jan 13, 10:09 pm, Icarus Sparry <use...(a)icarus.freeuk.com> wrote: >> On Wed, 13 Jan 2010 09:29:47 -0800, Pankaj wrote: >> >> > If you are using a reasonably recent ksh93, then you can ignore Ed's >> >> > advice as ksh93 can do it with builtin commands. >> >> >> I get where you're coming from, but just because you can apparently do >> >> this one thing with some constructs specific to some versions of ksh93 >> >> doesn't mean it's a bad idea to install and use gawk for this and your >> >> future text processing needs instead. >> >> >> Ed. >> >> >> The key is to note that >> >> >> > printf "%(%#)T" now >> >> >> > will give you the time in seconds since the epoch, and you can >> >> > substitute any reasonable phrase for "now", like "last monday". >> >> >> > So something like >> >> >> > while read task day time >> >> > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now) >> >> > if ((age> 2*3600)) >> >> > then printf "%s is %d seconds old\n" "$task" $age fi >> >> > done >> >> >> > should do it.- Hide quoted text - >> >> >> - Show quoted text -- Hide quoted text - >> >> >> - Show quoted text - >> >> > Icarus: There seems to be some problem as I keep getting syntax error. >> > Honestly, I truly did not understood how to above code works but just >> > wanted to try to see if this resolves the issue >> >> > This is what I tried >> >> > #!/usr/bin/ksh >> >> > while read task day time >> > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" "last monday") >> > if ((age > 2*3600)) >> > then printf "%s is %d seconds old\n" "$task" $age fi >> > done < File1.dat >> >> > test.ksh[3]: ()T-%#)T: syntax error >> >> What version of ksh are you running? You need a reasonably up-to-date >> ksh93. What does >> >> printf "%T\n" now >> >> say when you are running inside ksh? It should print out the date. >> What does >> >> echo "${.sh.version}" >> >> say, it should give something like >> >> Version JM 93t+ 2009-12-18 >> >> If there is nothing, or you get /usr/bin/ksh: ${.sh.version}: bad >> substitution then you are probably running ksh88 (and are probably on a >> sun). Often in that case you can get away with running /usr/dt/bin/dtksh, >> but unfortunately not in this case. dtksh is a ksh93 but not recent >> enough. >> >> So in this case you probably have to fall back to this >> >> http://groups.google.com/group/comp.unix.shell/msg/b17011dbda4bb76b >> >> and in particular the timegm function.- Hide quoted text - >> >> - Show quoted text - > > Thanks everyone. I was able to come up with following which is working > pretty good. > > while read val1 val2 val3 > do > > start=$val3 > end=`date +%H:%M:%S` > > ((start_mins = $(expr substr "$start" 1 2)*60 + $(expr substr "$start" > 4 2))) > ((end_mins = $(expr substr "$end" 1 2)*60 + $(expr substr "$end" 4 > 2))) > ((elapsed_mins = end_mins - start_mins)) > > if [[ "$elapsed_mins" -lt 0 ]] > then > ((elapsed_mins += 1440)) > fi > > if [[ "$elapsed_mins" -gt 120 ]] > then > print $((elapsed_mins / 60)) hours and $((elapsed_mins % 60)) minutes > fi > > done < file.txt Won't your algorithm break as you cross midnight boundaries? Regardless, if you have GNU date you could do something like: NOWSEC="$(date +%s)" while read taskid ts; do THENSEC="$(date -d "$ts" +%s)" if [ "$(( NOWSEC - THENSEC ))" -gt "$((2*60*60))" ]; then echo "${taskid} is over 2 hours old" fi done < /tmp/test.txt
First
|
Prev
|
Pages: 1 2 Prev: absg Next: zsh or ksh glob for directories and symlinks to directories |