Question

I have a directory tree with a bunch of folders and subfolders in it and a bunch of files in those sub folders. I need to get the count of the word 'Hello' in each of those files and display the result as 'File 'a' has 'Hello' n times', 'File 'b' has 'Hello' m times' and so on. How do i write a script in linux, so that i can get the word count for the word hello in each of these files. I have tried some grep combinations with 'wc' after browsing through net , but that gave me the total count from all the files in all the folders.

Thanks

Was it helpful?

Solution

Try something like this:

find . -type f | xargs -n1 grep "Hello" -c

Adding -type f to find ensures that it only returns files, not directories. Adding -n1 to xargs makes it so that every file returned by find gets its own invocation of grep, so that you can get a per-file count. The -c argument to grep returns the count of matches instead of every match.

The above expression will count the number of lines that have 'Hello' in them. If you need the total number of Hellos, instead of just the number of lines that have Hello in them, you'll need to do something more sophisticated. You can use the -o option on grep to just print the matching section of a line, and then combine that with wc -l to get the number of total occurrences.

OTHER TIPS

Using grep the syntax is:

grep -Rc "Hello" your_dir/

I also recommend ack as an excellent replacement for grep:

ack -lc "Hello" your_dir/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top