Favorite Color
November 11, 2014
Little database problems like this can be solved by big Scheme programs or little Awk programs; despite my fondness for Scheme, I solve problems like this using Awk:
awk ' /^favoritecolor: / { color[$2]++ }
END { for (c in color) print color[c], c } ' database |
sort -rn |
sed '1q' |
awk ' { print $2 } '
The first line finds all favoritecolor
database fields and counts the occurrences of each color. The second line prints each color/count combination on a separate line. The third line sorts in reverse numeric order. The fourth line selects the first (maximal) color. The fifth line strips the count and prints the color. Easy to do, and written as fast as I can type.
I wrote this little exercise because I had a problem at work today that could be solved in a manner similar to this. It’s a reminder that we don’t always need big programs; sometimes, a little program will do the job just as well.
You can see the program at http://programmingpraxis.codepad.org/cFVZnPVP. If you have some other little program, you might want to share it with the rest of us.
Or you can do it in perl…
Nah. I thought of Awk, and I thought of Python’s Counter objects, but this is what I would actually do, including the output of more than one line, to see if there is a tie or a near tie.
$ grep -E '^favoritecolor:' particular.txt | cut -d ' ' -f 2 | sort | uniq -c | sort -nr | head
Unfortunately, my UNIX command line is so rusty, I’d use Python.
Assumes one name/value pair per line in the file (problem doesn’t specify). Didn’t bother splitting the name/value pair, because it doesn’t change the counts. Outputs a sorted list of all the colors and their counts.