Groovy-Script to count each character in passed file and prints them in ascending order
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
#! /usr/bin/env | |
// Example | |
// groovy countEachChar.groovy MY_TEXTFILE > OUT_PUT_TEXT_FILE | |
def printErr = System.err.&println | |
if (args.length == 0) { | |
printErr "No path is passed as an argument! Exiting!" | |
return; | |
} | |
File file = new File (args[0]) | |
def map = [:] | |
file.eachLine { line -> | |
line.toCharArray().each { c -> | |
if (map.containsKey(c)) { | |
map.put(c, map[c] + 1) | |
} else { | |
map.put(c, 1) | |
} | |
} | |
} | |
// asc | |
map.sort { e1, e2 -> | |
e2.value <=> e1.value | |
}.each { k, v -> | |
println "$k $v" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output