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!

denombrate

Status
Not open for further replies.

mslider

Programmer
Jun 24, 2005
9
FR
Hi,

I have a text file formated as below:

yannick 2 1 5 2 1 0 3 1 6 8
anna 0 0 0 0 0 0 1 0 0 0
paul 2 0 0 0 0 0 0 0 0 0
bob 3 1 0 2 3 6 4 8 6 4
holyanna 4 0 0 0 0 0 0 0 0 0
samantha 0 0 0 0 0 5 0 0 0 0
richard 0 1 0 0 0 0 0 0 0 0
fred 0 0 3 0 0 0 0 0 0 0
tony 1 0 0 0 0 0 0 0 0 0
steve 4 2 0 5 6 2 0 0 0 0
andrea 0 0 0 0 0 0 0 2 0 0
bill 0 0 0 0 0 5 0 0 0 0

and I want to comptabilise how many times an unique single value appear on each line, so i used this code to do that:

{
for(x=1;x<=2;x++){
cpt=tot=0
for(i=2;i<=NF;i++){
tot+=$i
if($i==x){cpt++}
}

if(cpt==1 && tot==x){tab[x]++;cpt=tot=0}
}
}

END{
for(j in tab){
print j, " : ", tab[j]
}
}

is there another way more valuable to perform it ?
 
That's not a bad way. This method would stop looking as soon as it finds a duplicate non-zero value.

[tt]{
found=0
for (i=2; i <= NF; i++) {
if ($i > 0) {
if (found > 0) { next } else { found=$i }
}
}
if (found > 0) { tab[found]++ }
}[/tt]

Annihilannic.
 
And what about this ?
{ $1="";gsub(/ 0/," ");if(NF==1)++tab[$1] }
END{ for(j in tab) print j " : " tab[j] }

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