Skip to content

Instantly share code, notes, and snippets.

@hadley
Created April 25, 2014 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadley/11288250 to your computer and use it in GitHub Desktop.
Save hadley/11288250 to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript
n <- as.integer(commandArgs(trailingOnly = TRUE))
lines <- readLines(file("stdin"))
words <- tolower(unlist(strsplit(lines, "\\W+")))
counts <- sort(table(words), decreasing = TRUE)
counts_n <- counts[1:n]
cat(sprintf("%8d %s\n", counts_n, names(counts_n)), sep = "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment