No output from awk command


 
Thread Tools Search this Thread
Operating Systems Solaris No output from awk command
# 1  
Old 02-16-2015
Code 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
# 2  
Old 02-16-2015
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.
Code:
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

Last edited by RavinderSingh13; 02-16-2015 at 06:00 AM.. Reason: Added a comment about awk
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-16-2015
Hi,
In awk, Begin body is execute before file reading, so no fields is setting at this moment.

Regards.
This User Gave Thanks to disedorgue For This Post:
# 4  
Old 02-16-2015
Yes I do agree with disedorgue here. From man awk as follows.
Quote:
BEGIN and END are two special kinds of patterns which are not tested against the input. The action parts of all BEGIN patterns are merged as if all the statements had been written in a single BEGIN block. They are executed before any of the input is read.
Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep output to awk command

Hi Team(Solaris 5.8/Ksh), How can we save grep output to awk variable when grep returns more than one line or word. abc.log # more abc.log Hi Makarand How r u bye Makarand Hello when grep returns only 1 word below command works nawk -v var=`cat abc.log |grep "Hello"` 'BEGIN { if... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

2. Shell Programming and Scripting

Different output for awk command on Linux & HP-UX

I am using an awk command to extract a particular portion of a string. Below is the command and its output on a Linux system: oracle@host1:/tmp (/home/oracle) $uname -a Linux host1 2.6.32-279.39.1.el6.x86_64 #1 SMP Fri Nov 15 05:38:26 EST 2013 x86_64 x86_64 x86_64 GNU/Linux ... (7 Replies)
Discussion started by: veeresh_15
7 Replies

3. Shell Programming and Scripting

Use of awk to filter out the command output

Hi All, I am trying to find out number of cores present for hp-ux server from the output of print_manifest (as shown below). i suppose awk will be best tool to use for filtering. output of print_manifest is : System Hardware Model: ia64 hp Integrity Virtual Partition ... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

4. Shell Programming and Scripting

usage of Awk command for output

Hi Experts, I have a Text file generated as below; <NAME> NEW#<technicalName><TAB> <Version> OLD#<technicalName><TAB> <Version> e.g. CH_PPV_AUDIT_DISTRIBUTOR NEW#EL_CFG_FTP_DISTRIBUTOR 2.1.0.upc2 OLD#EL_CFG_FTP_DISTRIBUTOR 2.1.0.upc1... (19 Replies)
Discussion started by: rajangupta2387
19 Replies

5. Shell Programming and Scripting

Format output in AWK command

hi Friends , I have a file as below s.txt 1~2~~4 2~6~~7 3~8~~9 t.txt 1~2~~4 2~5~8~7 3~8~~7 header for both files is common (2 Replies)
Discussion started by: i150371485
2 Replies

6. UNIX for Dummies Questions & Answers

taking the output of awk command to a new file

cat doc | nawk -v da="${date}" '$23>199 {print $0 > "doc"+da+".txt"}' Every time(need to run every day) i run this, i want to a create a new file "doc_01 Aug.txt". Basically, i want to create a new file with date appended in it. The above command is creating a file with name "0".... (4 Replies)
Discussion started by: vagar11
4 Replies

7. Shell Programming and Scripting

awk command : To print the output to a file

half of the problem is already solved with the help of bartus11 suggestion I have a txt file having rows and coulmns, i want to perform some operation on a specific coulmn starting from a specific line. 50.000000 1 1 1 1000.00000 1000.00000 ... (5 Replies)
Discussion started by: shashi792
5 Replies

8. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

9. Shell Programming and Scripting

Format Output with AWK command

Hi - I have a file with contents as below. 12.1 a.txt 12.1 b.txt 12.1 c.txt 13.2 a.txt 13.2 d.txt 14.3 f.txt 15.4 a.txt 15.4 b.txt 15.4 z.txt I need to print the contents like this. 12.1 a.txt <&nbsp><&nbsp><&nbsp>b.txt <&nbsp><&nbsp><&nbsp>c.txt (7 Replies)
Discussion started by: guruparan18
7 Replies

10. Shell Programming and Scripting

space in output from awk command

Hi I have tried this command cat /etc/passwd | awk -F: '{print$5}' note that the $5 is the column that displays full name- i.e. Kevin Kambell To the point, my output is fine except one thing I do not understand that some of output lines have space in front of them which I checked in... (7 Replies)
Discussion started by: lalelle
7 Replies
Login or Register to Ask a Question