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

Time:10-01

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

CodePudding user response:

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

cat * | grep -Eo '[[:alnum:]] ' | sort | uniq -c | sort -rn | sed 1q
  • Related