Count occurrences

2 snippets across 2 stacks - Bash & Linux, Python

SHBash & Linux

Filter Duplicate Lines

SH · File Content
Syntax
uniq [options] [input [output]]
Example
sort access.log | uniq -c | sort -rn | head -20
Output
    847 GET /api/users
    623 GET /api/health
    412 POST /api/login

Note uniq only removes adjacent duplicates, so you almost always need to sort first. -c prefixes each line with its count, -d shows only duplicates, -i ignores case.

PYPython

defaultdict

PY · Dictionaries
Syntax
from collections import defaultdict
dd = defaultdict(factory)
Example
from collections import defaultdict

word_count = defaultdict(int)
for word in "the cat sat on the mat".split():
    word_count[word] += 1
print(dict(word_count))
Output
{'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}

Note defaultdict auto-creates missing keys using the factory function. Common factories: int (0), list ([]), set (set()).

Frequently asked questions

How do you count occurrences?
This task is covered in 2 stacks on this page: Bash & Linux, Python. The "Filter Duplicate Lines" snippet in Bash & Linux uses `uniq [options] [input [output]]`.
Which command does the Bash & Linux example use?
The "Filter Duplicate Lines" snippet uses `uniq [options] [input [output]]`, from the File Content section of the Bash & Linux cheat sheet.
Which stacks cover "count occurrences" on this page?
Bash & Linux, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Filter Duplicate Lines": uniq only removes adjacent duplicates, so you almost always need to sort first. -c prefixes each line with its count, -d shows only duplicates, -i ignores case.