Last active
July 5, 2016 19:26
-
-
Save r351574nc3/8e9863ec094c933ffbb1ad1eed8dab96 to your computer and use it in GitHub Desktop.
Functions that count lines in a file, then creates a new file sorted by those line counts.
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
import scala.collection.mutable.{Map} | |
import scala.io.{Source} | |
import java.io.{File, PrintWriter} | |
import scala.util.{Sorting} | |
object linecount { | |
def run(path:String) = { | |
var frequencies:Map[String, Int] = Map() | |
for (line <- Source.fromFile(path).getLines) { | |
var count = 1 | |
if (frequencies.contains(line)) { | |
count += frequencies.get(line).get | |
} | |
frequencies.put(line, count) | |
} | |
val sorted_keys = frequencies.keys.toStream.sortWith((a, b) => frequencies(a) < frequencies(b)) | |
var newpath = path + ".sorted" | |
var p = new PrintWriter(newpath) | |
try { | |
sorted_keys.foreach(x => { | |
var sentence = x | |
p.println(frequencies.get(sentence).get + " " + sentence); | |
}) | |
} | |
catch { | |
case e:Exception => { | |
println("Error writing file " + newpath) | |
} | |
} | |
finally { | |
try { | |
p.close(); | |
} | |
catch { | |
case e:Exception => {} | |
} | |
} | |
} | |
def main(args: Array[String]) { | |
if (args.length < 1) { | |
println("A filename is required") | |
return | |
} | |
val path = args(0) | |
if (path == null || path.isEmpty()) { | |
println("Please give a valid filename") | |
} | |
run(path) | |
} | |
} |
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
#!/bin/sh | |
count() { | |
FILENAME=$1 | |
while read x; do | |
echo $(grep -caF "$x" $FILENAME) $x | |
done < $FILENAME | sort -n | uniq> "$FILENAME".sorted | |
} | |
count $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment