4 ms·
Is this a nerd sniping thing? It has been a while since I've wrestled with Perl, but I suppose the recursive file search would keep it from being a one-liner.
by flubert 6y ago
Is this a nerd sniping thing? It has been a while since I've wrestled with Perl, but I suppose the recursive file search would keep it from being a one-liner. 4-5 "reasonable" lines maybe? Definitely less than 80 characters? I see I don't even have Perl installed on this machine, oh well.
- jeffbee 6y agoIt does seem like a weird choice, since anyone faced with this problem fewer than a billion times will surely choose shell tools. In python it's pretty short, and I don't even know python. from collections import defaultdict ...: d=defaultdict(int) ...: for fn in glob.iglob("**/*.txt"): ...: with open(fn, 'r') as f: ...: for line in f: ...: for word in line.split(): ...: d[word] = d[word] + 1 ...: [(k, v) for k, v in sorted(d.items(), key=lambda x: x[1], reverse=True)][:10] That said, the C++ version in the article could be tightened up a bit. What's happening on line 34, for instance?
- tzs 6y agoI'd probably just do it with existing commands (pipe split across multiple lines to make it easier to read): find . -type f -name '*.txt' -print0 | xargs -0 cat | tr ' \t' '\n\n' | grep '\S' | sort | uniq -c | sort -nr | sed 10q I'm assuming that when it says find all the .txt files in subdirectories of the current directory, that should include .txt files in the current directory.