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!

Getting Awk to Add Zeroes 1

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
I have a simple awk script that I am trying to get awk script to print 0 (zero) when it does not find any quantities as it searches a data file. Here is an example script

BEGIN { print "nuts", "apples" }
$1 == nuts && $8 == apples
{found++}
END { print $1, $8}

I have tried inserting the following before the last print statement, but could not get it to work:

if (found = "")
found = 0
else found = found

thanks
thunderkid
 
Something like this ?
BEGIN { print "nuts", "apples"; found=0 }
$1 == "nuts" && $8 == "apples"
{found++}
END { print found}


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Another way, to force the unitialized variable found to print as zero:
printf "%d\n",found

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
PH
The first solution worked. I will have to give the second solution a try. You deserve a star! Have a good weekend.
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top