Created
November 12, 2012 11:49
-
-
Save maxidr/4058929 to your computer and use it in GitHub Desktop.
Ruby as AWK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby -n option: Causes Ruby to assume the following loop around your script, which makes it iterate over file name | |
# arguments somewhat like sed -n or awk. | |
# | |
# Example: | |
curl -s http://www.gutenberg.org/files/1080/1080.txt | | |
ruby -ne ' | |
BEGIN { $words = Hash.new(0) } | |
$_.split(/[^a-zA-Z]+/).each { |word| $words[word.downcase] += 1 } | |
END { | |
$words.each { |word, i| printf "%3d %s\n", i, word } | |
} | |
' | | |
sort -rn | |
# from http://tomayko.com/writings/awkward-ruby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and the -p option: Acts mostly same as -n switch, but print the value of variable $_ at the each end of the loop.
For example:
or