How to Count Occurrences of a Word in All the Files of a Directory

How to count occurrences of a word in all the files of a directory?

grep -roh aaa . | wc -w

Grep recursively all files and directories in the current dir searching for aaa, and output only the matches, not the entire line. Then, just use wc to count how many words are there.

How to get occurrences of word in all files? But with count of the words per directory instead of single number

Update: The following command can be used on AIX:

#!/bin/bash

for name in /path/to/folder/* ; do
if [ ! -d "${name}" ] ; then
continue
fi
# See: https://unix.stackexchange.com/a/398414/45365
count="$(cat "${name}"/error*.log | tr '[:space:]' '[\n*]' | grep -c 'SEARCH')"
printf "%s %s\n" "${name}" "${count}"
done

On GNU/Linux, with GNU findutils and GNU grep:

find /path/to/folder -maxdepth 1 -type d \
-printf "%p " -exec bash -c 'grep -ro 'SEARCH' {} | wc -l' \;

Replace SEARCH by the actual search term.

Count occurrence of list of words in multiple files

Take the output from you script and pipe it to

awk '{ arry[$1]+=$2 } END { for (i in arry) { print i" "arry[i] } }' 

Count all occurrences of a string in lots of files with grep

cat * | grep -c string

How to count occurrences of a word in all the files of a directory? But with count incremented only once per word per file

So what you want is the number of files that contain that word. Easy:

grep -l word *|wc -l

Count word occurrence for all the files in a folder using Powershell

It turns out I missed the left parenthesis for the command

 (Get-ChildItem -Filter "*.manifest" -Recurse | Select-String -pattern "D1" -AllMatches).matches.count 

Works fine for me.



Related Topics



Leave a reply



Submit