Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you find the most common words in multiple files in unix?

I have 3 files in my current directory and I need to find the overall most frequent word. How do I do that? I am only able to find the most frequent word from each of the files:

$ for file in *; do 
    printf '%s : %s\n' "$(grep -Eo '[[:alnum:]]+' "$file" | sort | uniq -c | 
        sort -rn | head -n1)" "$file" 
done

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Concatenate all the files into a single stream and use the same pipeline:

cat * | grep -Eo '[[:alnum:]]+' | sort | uniq -c | sort -rn | sed 1q
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading