I have a program that writes on the standard output a list of string values, one per line, and I would like to display in real time the list of distinct values along with the number of occurences for each one.For example, from this output:applecarrotpearcarrotappleI need a command which generates this output (ideally updated in real time):apple: 2carrot: 2pear: 1Any idea?
Display distinct values of a list and number of occurences
command line;text processing;sort
To expand on what @Bratchley said in the comments, if you have your program's output printing to a file, then you can run then watch command in the terminal to get near-real-time view of the output by including the -n flag like so:watch -n 0.1 cat yourprograms.log | sort | uniq -c | sort -rnNote: The ' -n ' flag sets the refresh interval. The minimum time ' watch ' can take is 0.1 second (or 1/10 a second) and nothing smaller.Example Output:Every 0.1s: cat yourprograms.log | sort | uniq -c | sort -rn      6 things      4 mine      3 dot      1 belowIncluding | sort -rn allows for a better sort view. sort -rn sorts the output of uniq -c in reverse numeric order. If you only care about say the top 10, you can include the head command like so:watch -n 0.1 cat yourprograms.log | sort | uniq -c | sort -rn | head