Skip to content

Instantly share code, notes, and snippets.

@BiruLyu
Created June 3, 2017 23:31
Show Gist options
  • Save BiruLyu/7bed471c5f3b37450a97d5cdc15d4414 to your computer and use it in GitHub Desktop.
Save BiruLyu/7bed471c5f3b37450a97d5cdc15d4414 to your computer and use it in GitHub Desktop.
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r| awk '{print $2, $1}'
#tr -s: truncate the string with target string, but only remaining one instance (e.g. multiple whitespaces)
#sort: To make the same string successive so that uniq could count the same string fully and correctly.
#uniq -c: uniq is used to filter out the repeated lines which are successive, -c means counting
#sort -r: -r means sorting in descending order
#awk '{ print $2, $1 }': To format the output, see https://linux.cn/article-3945-1.html.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment