Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove NULLS from count

Status
Not open for further replies.

rhusain

IS-IT--Management
Oct 1, 2006
3
US
I have a file that looks like this-

PSM 22 DEFECT OPEN
PSR 19 DEFECT OPEN
PSM DEFECT OPEN
PSQ 20 DEFECT OPEN
PSM 24 DEFECT OPEN

When I run this command -

printf '%d\n' `cat <file_name> | awk '{if($2>20) print $2}' | wc -l'

I get count of 3, which means NULLS are getting counted. How can I stop the nulls to be included in the total count?

Thanks,

Riyaz
 
awk doesn't know empty or null fields, so on line

PSM DEFECT OPEN

$2 is set to "DEFECT" which evaluates to true in your test ($2>20).

try this awk program:

awk '{if((NF==4)&&($2>20)) print $2}'


HTH,

p5wizard
 
Hi,

I tried awk '{if((NF==4)&&($2>20)) print $2}'
it returns 0 rows. No error is returned.

Thanks,

Riyaz
 
p5's solution works fine for me. What is the output of od -c yourtestfile? What operating system and version of awk?

Annihilannic.
 
And what about this ?
awk '$2~"^[0-9]+$" && $2>20{++i}END{print i}' file_name

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

I applied your example on the following file -

ProSource 2006.1 21 Compare Viewer
ProSource 2006.1 18 PTM GUI
ProSource 2.5 20 Server Defect
ProSource 3.0 Finder Adaptor Defect
ProSource 3.0 22 Compare Viewer
ProSource 2.0 17 OpenWorks Adaptor
ProSource 2006.1 14 GIS Viewer
ProSource 2.0 16 GIS Viewer
ProSource 2007.1 25 General Defect
ProSource 3.0 Usability Defect OPEN
ProSource 3.0 19 Installation Defect
ProSource 3.0 Tree Viewer Defect

printf '%d\n' `cat out1.txt | awk '{if((NF==4)&&($3>20)) print $3}' | wc -l`

I get "0" rows. I expect "3".

I also tried -

awk '$3~"^[0-9]+$" && $3>20{++i}END{print i}' out1.txt

I get -
awk: syntax error near line 1
awk: bailing out near line 1

Regards,

Riyaz
 
Either use nawk if available, or try this:
awk '$3~"^[1-9][0-9]*$" && $3>20{++i}END{print i}' out1.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top