Tobias Lange
Tobias Lange

Reputation: 13

Counting the number of occurrences of a character in multiple files with unix shell

I would like to help out my girlfriend - she needs the specific count of certain characters in around 200 files (per file).

I already found How can I use the UNIX shell to count the number of times a letter appears in a text file?, but that only shows the complete number, not the number of occurrences per file. basically, what I want is the following:

$ ls 
test1   test2
$ cat test1
ddddnnnn
ddnnddnnnn
$ cat test2
ddnnddnnnn
$ grep -o 'n' * | wc -w
16
$ <insert command here>
test1 10
test2 6
$

or something similar regarding the output. As this will be on her university machine, I cannot code anything in perl or so, just shell is allowed. My shell knowledge is a bit rusty, so I cannot come up with a better solution - maybe you could be of assistance.

Upvotes: 1

Views: 2825

Answers (3)

jim mcnamara
jim mcnamara

Reputation: 16389

Glen's answer is far better for the flavors of UNIX that support it. This will work on a UNIX that claims it is POSIX-compliant. This is meant for the poor folks for whom the other answer does not fly. POSIX grep says nothing about grep -H -o See: http://pubs.opengroup.org/onlinepubs/009604499/utilities/grep.html

Get a list of the files you want call it list.txt. I chose the character ^ == shift 6 for no reason

while read fname
do
  cnt=`tr -dc '^' < $fname | wc -c`
  echo "$fname: $cnt"
done < list.txt

Upvotes: 0

tchap
tchap

Reputation: 1057

It's not exactly elegant, but the most obvious solution is:

letter='n'
for file in *; do
    count=`grep -o $letter "$file" | wc -w`
    echo "$file contains $letter $count times"
done

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246827

grep -Ho n * | uniq -c

produces

 10 test1:n
  6 test2:n

If you want exactly your output:

grep -Ho n * | uniq -c | while read count file; do echo "${file%:n} $count"; done

Upvotes: 2

Related Questions