No output from awk command

Hi all,

In my SunOS 5.10, the command awk using BEGIN option doesn't print output variables like expected
here is my test:

with the following code everything is alright
ps -eo pcpu,args | grep "httpd" | awk ' { print $1 } '
the result is
0.1

However, with this command
ps -eo pcpu,args | grep "httpd" | awk 'BEGIN {cpu=$1} END { print cpu }'
the result is
BLANK SPACE

Hello Elmassimo,

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk , this should work then.

EDIT: Also why not use print in spite of using BEGIN and END .

ps -eo pcpu,args | grep "httpd" | /usr/xpg6/bin/awk '{print "cpu =" $1}'
OR
ps -eo pcpu,args | grep "httpd" | /usr/xpg4/bin/awk '{print "cpu =" $1}'

Thanks,
R. Singh

1 Like

Hi,
In awk, Begin body is execute before file reading, so no fields is setting at this moment.

Regards.

1 Like

Yes I do agree with disedorgue here. From man awk as follows.

Thanks,
R. Singh