Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:15:27+00:00 2026-06-13T01:15:27+00:00

I need to search through a directory which contains many sub directories, each which

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:15:28+00:00Added an answer on June 13, 2026 at 1:15 am

    If I understood what you mean, and you want to get a list of items, sorted by frequency, you can pipe through something like:

    | sort | uniq -c | sort -k1nr
    

    Eg:

    Input:

    file1
    file2
    file1
    file1
    file3
    file2
    file2
    file1
    file4
    

    Output:

    4 file1
    3 file2
    1 file3
    1 file4
    

    Update

    By the way, what are you using awk for?

    find . -name 'quest*' | cut -d_ -f1  | sort | uniq -c | sort -k1nr | head -n10
    

    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 🙂

    find . -name 'question*' | sort \
        | sed "s#\(.*/question\([0-9]\+\)_[0-9]\+\)#\2 \1#" \
        | awk '{ cnt[$1]++; files[$1][NR] = $2 } END{for(i in files){ print i" ("cnt[i]")"; for (j in files[i]) { print "    "files[i][j] } }}'
    

    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:

    #!/usr/bin/env python
    
    import sys, re
    
    file_re = re.compile(r"(?P<name>.*/question(?P<id>[0-9]+)_[0-9]+)")
    
    counts = {}
    files = {}
    
    if __name__ == '__main__':
        for infile in sys.stdin:
        infile = infile.strip()
        m = file_re.match(infile)
        _name = m.group('name')
        _id = m.group('id')
        if not _id in counts:
            counts[_id] = 0
        counts[_id]+=1
        if not _id in files:
            files[_id] = []
        files[_id].append(_name)
    
        ## Calculate groups
        grouped = {}
        for k in counts:
        if not counts[k] in grouped:
            grouped[counts[k]] = []
        grouped[counts[k]].append(k)
    
        ## Print results
        for k, v in sorted(grouped.items()):
        for fg in v:
            print "%s (%s)" % (fg, counts[fg])
            for f in sorted(files[fg]):
                print "    %s" % f
    

    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, create setup.py:

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    
    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = [Extension("calculate2", ["calculate2.pyx"])]
    )
    

    And a launcher script (I named it calculate2_run.py)

    import calculate2
    import sys
    if __name__ == '__main__':
        calculate2.runstuff(sys.stdin)
    

    Then, make sure you have cython installed, and run:

    python setup.py build_ext --inplace
    

    That should generate, amongst other stuff, a calculate2.so file.

    Now, use calculate2_run.py as 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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to search through a directory, ARCHIVE, which contains many sub directories, each
I am trying to recursively search through a directory for all sub-directories within any
I need a regular expression utility that will search through a specified directory and
I use file_get_contents(http://www.mydomain.com); to get the content. Then I need to search through it
I need to be able to search through multiple 2D arrays and search the
This below goes through files in a directory, reads them and saves them in
Is there an easy way for me to automatically search recursively through a directory
Hey everyone. I need to write a POSIX program to search through an entire
I need to be able to Search a directory and its subdirectories for specific
I need to search for certain users on Facebook by their names(ex. Full name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.