Count occurences of character X in all files in current directory

Count occurences of character X in all files in current directory

If you ever need to count the occurences of a character in all (or some) files, this next series of commands would come in handy:

for f in `ls`; do cat $f; done | awk '{ for ( i=1; i<=length; i++ ) arr[substr($0, i, 1)]++ }END{ for ( i in arr ) { print i, arr[i] } }' | grep X

Replace X with the character in question. The command is all in one line. “ls” can be replaced with a more specific list (eg: “ls *.c“)

Leave a Reply