Problems with AWK

Hi I'm a newbie to Unix scripting and was having some problems with AWK. I have written this little script that should read a process list and then print out the PID's of the offending processes. Unfortunately it doesn't seem to work! The script is as follows:

ps -ef | awk '{if ((substr($5,1,length($5)-6) -0) - ((system (�date '+%H' �)) -0) >= 12) print $2}'

When I execute the script the "((system (�date '+%H' �)) -0)" part of the script is, for some reason, not executed. Could some one shed some light on the matter.

Cheers,
A. Newbie

try this:

system (date +"%H")) -0)

well..... several probelms here:

  1. here's sample output of my 'ps -ef' [Solaris]:
    root 175 1 0 Nov 23 ? 0:00 /usr/sbin/inetd -s
    root 14114 14104 0 09:30:17 pts/2 0:00 ps -ef

    As you can see the '$5' for the first record/line is now what your script is expecting

  2. here's the snippet from 'man nawk':

 I think you're expecting something else - not the 'return code', but the actual value of the current hour. For that I would use the follwing paradigm:
     cmd="date '+%H'"
     cmd | getline currentHour
     close(cmd) 

Thanks vgersh for correcting me.

Just for more information

adm 852192 1171598 0 09:00:00 - 0:00 /usr/lib/sa/sadc 1200 3 /var/adm/sa/sa14
oracle 868576 1 0 Feb 10 - 0:02 oracleXXXXXX (LOCAL=NO)
oracle 925834 999576 2 09:22:52 pts/10:00 ps -ef
root 950326 393380 0 08:00:46 - 0:00 /opt/soe/local/openssh/sbin/sshd -D
oracle 958696 1 0 Feb 10 - 0:00 oracleXXXXXX (LOCAL=NO)
oracle 983140 1 0 Feb 10 - 0:02 oracleXXXXXX (LOCAL=NO)
oracle 999576 1188022 0 08:01:04 pts/1 0:00 -ksh
adm 1171598 1 0 09:00:00 - 0:00 bsh /usr/lib/sa/sa1 1200 3
oracle 1188022 950326 0 08:01:04 - 0:00 /opt/soe/local/openssh/sbin/sshd -D

I get the following when I run the command ,im using AIX 5.2

300 $ ps -ef | awk '{if ((substr($5,1,length($5)-6) -0) - ((system ("date '+%H'"))-0) >= 12) print $2}'
09
09
09
09
09
09
09
09

Here is of what works and what doesn't. It seems as though is there is an actual integer as the second statement the script will work as expected. However if it has to call a value or execute a sub-script it won't function as intended.

nawk 'BEGIN {(((system ("date '+%H' "))-0))}'
PRINTS OUT HOUR

more testfile |awk '{if ( (15) - ((system (�date '+%H' �)) -0) >= 12) print $2}' KEEPS IGNORING 2nd STATEMENT!

time=15
more testfile |awk '{if ( (15) - ($time) >= 12) print $2}'
KEEPS IGNORING 2nd STATEMENT!!

time=15
more testfile |awk '{if ( ($time) - (15) >= 12) print $2}'
THIS SEEMS TO WORK!

more testfile |awk '{if ( ((system (�date '+%H' �)) -0) - (15) >= 12) print $2}' THIS WORKS!

....and what about:

nawk 'BEGIN { out=system("date '+%H'"); printf("hour->[%s]\n", out)}'

As I've quoted in my original reply..... "system" is not doing what you think it is.

Point taken but how do I incorporate the piece of script you suggested?

what "piece of script"?
This? This is just to get the current hour from within awk.

     cmd="date '+%H'"
     cmd | getline currentHour
     close(cmd) 

Yeah that code, how do I go about incorporating that? For example cmd="date '+%H'" is a varible that would be set outside of awk, right? So how do I incorporate the rest of the code? Would it be something like the following:

cmd=�date �+%H�
ps -ef | awk '{if ((substr($5,1,length($5)-6) -0) - (cmd | getline currentHour) ; close (cmd) -0) >= 12) print $2}'

#!/bin/ksh
currentHour=$(date '+%H')
ps -ef | nawk -v ch="${currentHour}" '{if (((substr($5,1,length($5)-6) -0) - ch) >= 12) print $2}'

Thanks a million, it works a treat!