Skip to content

Instantly share code, notes, and snippets.

@rsvp
Created February 18, 2012 15:43
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rsvp/1859875 to your computer and use it in GitHub Desktop.
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.
#!/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