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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count repeat occurances in a file

Status
Not open for further replies.

ranjit

Technical User
Apr 14, 2000
131
GB
[tt]
I'm looking for an awk routine to mimic the uniq -c command for the following:

$ cat input
A
A
A
A
B
B

$ cat script
{ A[NR]=$0 }

END{
for(i=1;i<=NR;i++) {
if (A ~/A/)
a++
else if (A ~/B/)
b++
}
print "A:" a
print "B:" b

}

$ awk -f script input
A:4
B:2

Clearly the above works when specifying the strings to be counted in the file: "A" and "B".

How is it done without the string specification e.g. as with the case of counting the repeat occurance of, say, 100 strings in a file.
[/tt]
 
Why not simply this ?
awk '{++a[$0]}END{for(i in a)print i,a}' input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV - I think i was over-complicating things
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top