Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the UNIX shell to count the number of times a letter appears in a text file?

I have a few text files and I'd like to count how many times a letter appears in each?

Specifically, I'd like to use the UNIX shell to do this, in the form of: cat file | .... do stuff...

Is there a way I can get the wc command to do this?

like image 445
samoz Avatar asked Sep 02 '09 15:09

samoz


People also ask

How do I count the number of times a word appears in Unix?

Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.

How do I count the number of times a word appears in Linux?

You can use the grep command to search strings, words, text, and numbers for a given patterns. You can pass the -c option to grep command. It only shows the number of times that the pattern has been matched for each file.

How do you count letters in Unix?

On Linux and Unix-like operating systems, the wc command allows you to count the number of lines, words, characters, and bytes of each given file or standard input and print the result.


2 Answers

grep char -o filename | wc -l 
like image 88
SilentGhost Avatar answered Sep 22 '22 21:09

SilentGhost


Another alternative:

tr -d -C X <infile | wc -c 

where X is the character or string of characters you want to count and infile is the input file.

like image 45
Scott Lindsay Avatar answered Sep 20 '22 21:09

Scott Lindsay