Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Power of Unix

Power of Unix

Why I love Unix. A Lightning Talk for Teach Us Something Technical
www.meetup.com/Teach-Us-Something-Technical/events/222877398/

Avatar for Scott Carleton

Scott Carleton

June 10, 2015

Other Decks in Programming

Transcript

  1. The Challenge Read a file of text, determine the n

    most frequently used words, and print out a sorted list of those words along with their frequencies.
  2. The Solution(s) A): Donald Knuth’s Literary Programming 10 Pages of

    ‘perfect’ Pascal B): Douglas McIlroy’s 6 lines of UNIX
  3. 1 tr -cs A-Za-z '\n' | 2 tr A-Z a-z

    | 3 sort | 4 uniq -c | 5 sort -rn | 6 head -n 1
  4. uniq -c Replace each run of duplicate words with a

    single representative and include a count (-c).
  5. All together now cat text.txt | tr -cs A-Za-z '\n'

    | tr A-Z a-z | sort | uniq -c | sort -rn | head -n 1 10 Pages of Pascal in 6 lines of Unix
  6. Note on Performance King James Bible: 4.2 M Text File

    794,915 Words time cat bible.txt | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | head -n 20 #=> real 0m11.918s Extra Credit: awk 'length($0)>4'