Problems with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with AWK
# 1  
Old 02-14-2005
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
# 2  
Old 02-14-2005
Quote:
Originally Posted by trainee
"((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)
# 3  
Old 02-14-2005
Quote:
Originally Posted by trainee
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
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':
Quote:
system(expression)
Execute the command given by expression in a
manner equivalent to the system(3S) function and
return the exit status of the command.
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:
Code:
     cmd="date '+%H'"
     cmd | getline currentHour
     close(cmd)

# 4  
Old 02-14-2005
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

Last edited by thumsup9; 02-14-2005 at 11:44 AM..
# 5  
Old 02-14-2005
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!
# 6  
Old 02-14-2005
....and what about:
Code:
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.
# 7  
Old 02-15-2005
Point taken but how do I incorporate the piece of script you suggested?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution Problems with awk

Ubuntu, Bash 4.3.48 Hi, I have this input file: a1:b2:c30:g4:h12:j7 and I want this output file: a1=g4:b2=h12:c30=j7 I can do it this with this code: awk -F':' '{print $1"="$4":"$2"="$5":"$3"="$6"}' INPUT > OUTPUTIn this case I have 6 columns, I calculate manually the half number of... (6 Replies)
Discussion started by: echo manolis
6 Replies

2. Shell Programming and Scripting

Having Problems with AWK

So, I'm having a lot of crazy problems with Awk that I cannot understand. This one in particular is driving me nuts. Here is one section of my Awk script: print $0 sub(/Jan/,"",$2) sub(/Feb/,"",$2) sub(/Mar/,"",$2) sub(/Apr/,"",$2) sub("May","",$2) ... (5 Replies)
Discussion started by: rrdein
5 Replies

3. Shell Programming and Scripting

awk problems - awk ignores conditions

awk 'BEGIN{ if('"$CATE"'<'"${WARN}"') printf ("%s", "'"`Kfunc "" ; break`"'") else if (('"${CATE}"'>='"${WARN}"') && ('"${CATE}"'<'"${CRIT}"')) printf ("%s", "'"`Wfunc ""; break`"'") else if ('"${CATE}"'>='"${CRIT}"') printf... (6 Replies)
Discussion started by: SkySmart
6 Replies

4. Shell Programming and Scripting

simple problems in awk

Dear All, I have the following awk script. #!/bin/bash sh stdev.cmd data.file | awk '{print $2}' > out.data read d < out.data echo $d awk '{print $1,$2- $f}' new > newz The script runs "stdev.cmd" and output a file "out.data" and the value of the... (2 Replies)
Discussion started by: Yacob_123
2 Replies

5. UNIX for Dummies Questions & Answers

Problems with AWK

Hi I am writing a shell script for a number of things and aone problem that keeps comming up is AWK formatting. When commands are typed into the command line they are fine, but when executed in the script the results are pilled up and not in a list/table format. I have tried using ... (2 Replies)
Discussion started by: AngelFlesh
2 Replies

6. Shell Programming and Scripting

Execution problems using awk command.

Hi All, I have the following requirement. In a directory i get files from external source. I at regular intervals check that directory for any incoming files. The file name is underscore delimited. Such as: aaa_bbb_ccc_ddd_eee_fff.dat I am using awk and and splitting the file name. ... (4 Replies)
Discussion started by: satishpv_2002
4 Replies

7. UNIX for Dummies Questions & Answers

problems with awk

Using Linux, I am trying to create a list of all the lines that have "Non-white" or "No" in column 3 of a file: ethnicity.txt. I have used the following command : awk '$3 == "No" || $3 == "Non-white" {print $1, $2, $3}' ethnicity.txt This only returns the lines with "No" and none of... (3 Replies)
Discussion started by: polly_falconer
3 Replies

8. Shell Programming and Scripting

help.. Problems in using awk

I do have a file (named as templist) which looks like this one: 00450000.000000 00402300.000000 00040000.000000 00020000.000000 00020000.000000 00020000.000000 00020000.000000 and I want to make a script that adds this using AWK or FOR. I tried using awk using the command but it just... (8 Replies)
Discussion started by: dakid
8 Replies

9. UNIX for Advanced & Expert Users

awk problems

awk ' FILENAME=="First"{ arr = 1; x=sub ; } FILENAME=="Second"{ if (/^10/ &&... (5 Replies)
Discussion started by: Ehab
5 Replies

10. Shell Programming and Scripting

awk problems

If i try the -f option for awk, i get the "awk: can't open " error message The following awk statement works fine without the -f option `awk <$RULES '/^IGNORE_POLICY / { print $2 }'` Below how i turned on debugging to show what is happening, can someone provide me with some advice!!!! ... (1 Reply)
Discussion started by: Junes
1 Replies
Login or Register to Ask a Question