Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created May 8, 2014 21:41
Show Gist options
  • Save unixpickle/0cf97ca78e521ef4d478 to your computer and use it in GitHub Desktop.
Save unixpickle/0cf97ca78e521ef4d478 to your computer and use it in GitHub Desktop.
Count occurrences of a line in a file
fs = require 'fs'
fs.readFile 'letters.txt', (err, buffer) -> processFile buffer.toString()
processFile = (s) ->
counts = {}
lines = s.split '\n'
for line in lines
if counts[line]?
counts[line]++
else
counts[line] = 1
console.log JSON.stringify counts, null, 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment