I need to search through a directory which contains many sub directories, each which contain files. The files read as follows question1234_01, where 1234 are random digits and the suffix _01 is the number of messages that contain the prefix, meaning they are apart of the same continuing thread.
find . -name 'quest*' | cut -d_ -f1 | awk '{print $1}' | uniq -c | sort -n
example output:
1 quest1234
10 quest1523
This searches for all the files then sorts them in order.
What I want to do is print all the files which end up having the most occurrences, in my example the one with 10 matches.
So it should only output quest1523_01 through quest1523_11.
If I understood what you mean, and you want to get a list of items, sorted by frequency, you can pipe through something like:
Eg:
Input:
Output:
Update
By the way, what are you using awk for?
Returns the 10 items found more often.
Update
Here it is a much improved version. Only drawback, it’s not sorting by number of occurrences. However, I’m going to figure out how to fix it 🙂
Update
After testing on ~1.4M records (it took 23”), I decided that awk was too inefficient to handle all the grouping stuff etc. so I wrote that in Python:
This one does all the job of splitting, grouping and sorting.
And it took just about 3” to run on the same input file (with all the sorting thing added).
If you need even more speed, you could try compiling with Cython, that is usually at least 30% faster.
Update – Cython
Ok, I just tried with Cython.
Just save the above file as
calculate2.pyx. In the same folder, createsetup.py:And a launcher script (I named it
calculate2_run.py)Then, make sure you have cython installed, and run:
That should generate, amongst other stuff, a
calculate2.sofile.Now, use
calculate2_run.pyas you normally would (just pipe in the results from find).I run it, without any further optimization, on the same input file: this time, it took 1.99”.