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?
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.
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.
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.
grep char -o filename | wc -l
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With