Skip to content

Instantly share code, notes, and snippets.

@bycoffe
Created September 25, 2009 13:40
Show Gist options
  • Save bycoffe/193538 to your computer and use it in GitHub Desktop.
Save bycoffe/193538 to your computer and use it in GitHub Desktop.
Count the number of occurrences of each line in a file.
#!/bin/bash
# Count the number of occurrences of each line in a file.
#
# Example:
#
# test.txt:
# A
# A
# B
# C
# D
# D
# D
#
# $ count < test.txt
# 2 A
# 1 B
# 1 C
# 3 D
#
# Often useful to sort the results:
# $ count < test.txt | sort -n
# 1 B
# 1 C
# 2 A
# 3 D
awk '{
c[$1]++
} END {
for (i in c) {
print c[i], i
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment