Created
February 18, 2012 15:43
-
-
Save rsvp/1859875 to your computer and use it in GitHub Desktop.
freqy.sh : sort lines by their frequency count -- a very useful idiom as Linux bash script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# bash 3.2.25(1) Ubuntu 7.10 Date : 2012-02-15 | |
# | |
# _______________| freqy : sort and order lines by frequency of appearance. | |
# | |
# Usage: freqy [optional: file(s)] | |
# | |
# Notes: Will work in a pipe. | |
# Does not alter the file(s) themselves. | |
# | |
# Dependencies: none | |
# | |
# CHANGE LOG LATEST version available: https://bitbucket.org/rsvp/gists/src | |
# | |
# 2012-02-15 Code review. | |
# 2009-04-30 First version of a very useful idiom. | |
# _______________ :: BEGIN Script :::::::::::::::::::::::::::::::::::::::: | |
cat "$@" | sort -f | uniq -c | sort -k 1nr -k 2f | |
# ^second field, case-insensitive | |
# ^highest numerical frequency first | |
# ^count frequency | |
# ^-f means case-insensitive | |
# ^needed to get similar lines adjacent to each other. | |
# ^With no FILE, or when FILE is -, cat reads standard input. | |
exit 0 | |
# _______________ EOS :: END of Script :::::::::::::::::::::::::::::::::::::::: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment